Destroy JSON object property for string

I have a JSON that has many properties, most of which are simple data types. However, I have one property in JSON that when I deserialize it to a C # class, I just need to deserialize it as a string.

JSON example:

{"simpleProperty": "value1", "json":{"a":"a1", "b":"b1"}}

      

The "json" object has no set structure, other than that it will be a valid JSON object.

So, in the above example, the "json" value is a JSON object, but when it gets deserialized, I need it as a string.

So if my class is C #:

public class MyClass
{
    public string SimpleProperty { get; set; }
    public string Json { get; set; }
}

      

And then if I use:

var myClass = JsonConvert.DeserializeObject<MyClass>(jsonStr);

      

I would like myClass.Json to just be a simple string.

I've looked at creating a custom JsonConverter to do this, but it seems too complicated for something so simple. I must be missing something. Any direction would be much appreciated.

I saw this post too, but it doesn't really answer the question: JSON.Net - How to deserialize JSON to an object, but treating a property as a string instead of JSON?

+3


source to share


2 answers


For my needs, I decided to go ahead and implement a custom JsonConverter like this:

    class JsonConverterObjectToString : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return (objectType == typeof(JTokenType));
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            JToken token = JToken.Load(reader);
            if (token.Type == JTokenType.Object)
            {
                return token.ToString();
            }
            return null;
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            //serializer.Serialize(writer, value);

            //serialize as actual JSON and not string data
            var token = JToken.Parse(value.ToString());
            writer.WriteToken(token.CreateReader());

        }
    }

      



I haven't fully tested the above implementation and I'm not entirely sure about the Canconvert method as it was never called, but it seems to do the conversion and then allows me to store the deserialized class in MongoDB and the JSON data is stored in a string. So it's okay. I found the following helpful in implementation: How to deserialize a JSON property that can be two different datatypes using Json.NET

UPDATE: Modified WriteJson method to serialize as JSON object (not string).

+3


source


Alternatively, you can use a JToken

property type to indicate that any valid JSON can go there. While this is not exactly what you asked for, it may be enough.

public class MyClass
{
    public string SimpleProperty { get; set; }
    public JToken Json { get; set; }
}

      



By doing myClass.Json.ToString()

, you will get JSON as string

.

+2


source







All Articles