2

I have an object that is written to Json using a custom Newtonsoft Jsonconverter. The object has two variables (array of vec3 points and array of triangleindices). The values need to be stored as base64 strings inside the json.

The writer works as expected, but I can't figure out how to read the data back in and recreate the object.

The object class definition:

public class Outline
{
    [JsonConverter(typeof(ObjectToBase64Converter))]
    public vec3[] Points { get; set; }
    [JsonConverter(typeof(ObjectToBase64Converter))]
    public int[] TriangleIndices { get; set; }
}

The custom Json converter:

internal class ObjectToBase64Converter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(string);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        // ?? I've got no clue ??

    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        using (MemoryStream memoryStream = new MemoryStream())
        {
            new BinaryFormatter().Serialize(memoryStream, value);
            string base64String = Convert.ToBase64String(memoryStream.ToArray());
            writer.WriteValue(base64String);
        }
    }
}

Any help would be greatly appreciated, I've been stuck on this for hours.

Schiette
  • 25
  • 4

1 Answers1

2

You could define it as following

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
    JToken token = JToken.Load(reader);
    // Convert the Base64 Value to Bytes
    var dataBytes = Convert.FromBase64String(token.Value<String>());
    // Deserialize the Binary Formatted Data
    using (MemoryStream ms = new MemoryStream(dataBytes))
    {
        IFormatter br = new BinaryFormatter();
        return Convert.ChangeType(br.Deserialize(ms),objectType);
    }
}

Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
  • Thanks for taking the time! I tried to compile but I'm getting an "type or namespace "token" could not be found". I searched google but couldn't find a clear answer. This is what I've included so far: using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization; Any ideas as to what I'm missing? – Schiette Mar 10 '20 at 15:29
  • @Schiette Did you mean JToken or token ? JToken is available in Newtonsoft.Json.Linq. If latter is the error, i guess you missed out one line " JToken token = JToken.Load(reader);" – Anu Viswan Mar 10 '20 at 15:33
  • I'm so sorry, I didn't look at your post carefully enough. Thanks for taking the time to help me out, it works perfectly now! – Schiette Mar 10 '20 at 15:38
  • @Schiette Glad to help you. Please do mark the answer as helpful/answer if it helped you resolve the issue https://stackoverflow.com/help/someone-answers – Anu Viswan Mar 10 '20 at 15:43