Skip to content

Added a json converter to deserialize base64 strings to a byte array #469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/Docker.DotNet/JsonBase64Converter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Docker.DotNet
{
internal class JsonBase64Converter : JsonConverter
{
public override bool CanRead => true;

public override bool CanWrite => false;

public override void WriteJson(JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
{
throw new NotImplementedException();
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
{
var strVal = reader.Value as string;

return Convert.FromBase64String(strVal);
}

public override bool CanConvert(Type objectType)
{
return objectType == typeof (IList<byte>);
}
}
}
7 changes: 4 additions & 3 deletions src/Docker.DotNet/JsonSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Newtonsoft.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Docker.DotNet
Expand All @@ -17,7 +17,8 @@ internal class JsonSerializer
new JsonVersionConverter(),
new StringEnumConverter(),
new TimeSpanSecondsConverter(),
new TimeSpanNanosecondsConverter()
new TimeSpanNanosecondsConverter(),
new JsonBase64Converter()
}
};

Expand All @@ -35,4 +36,4 @@ public string SerializeObject<T>(T value)
return JsonConvert.SerializeObject(value, this._settings);
}
}
}
}