Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON.Net - Change $type field to another name?

Tags:

json.net

When using Json.Net, I understand how to get the $type property into the rendered json, but is there a way to change that field name? I need to use "__type" instead of "$type".

like image 558
Brian McCord Avatar asked Feb 28 '12 21:02

Brian McCord


People also ask

How to set JsonProperty name in c#?

To set the name of individual properties, use the [JsonPropertyName] attribute. The property name set by this attribute: Applies in both directions, for serialization and deserialization.

What is JSON property name?

The Jackson Annotation @JsonProperty is used on a property or method during serialization or deserialization of JSON. It takes an optional 'name' parameter which is useful in case the property name is different than 'key' name in JSON.

What is JsonProperty annotation C#?

JsonPropertyAttribute indicates that a property should be serialized when member serialization is set to opt-in. It includes non-public properties in serialization and deserialization. It can be used to customize type name, reference, null, and default value handling for the property value.

What does Jsonconvert Deserializeobject do?

Deserializes the JSON to the specified . NET type. Deserializes the JSON to the specified . NET type using a collection of JsonConverter.


3 Answers

http://json.codeplex.com/workitem/22429

"I would rather keep $type hard coded and consistent."

Consistent with what I wonder?

http://json.codeplex.com/workitem/21989

I would rather not - I think this is too specific to me and I don't want to go overboard with settings. At some point I will probably implement this - http://json.codeplex.com/workitem/21856 - allowing people to read/write there own meta properties in the JSON and you could reimplement type name handling with a new property name. The other option is just to modify the source code for yourself to have that property name.

And more recently, Issue #36: Customizable $type property name feature:

I'd rather not

This is my solution...

json.Replace("\"$type\": \"", "\"type\": \"");
like image 50
Ian Warburton Avatar answered Nov 13 '22 04:11

Ian Warburton


Looks like this is hardcoded as public const string TypePropertyName = "$type"; in Newtonsoft.Json.Serialization.JsonTypeReflector which is internal static class unfortunately.

I needed this myself, and the only thing I can think of is having custom modified version of json.net itself. Which is of course is a major pita.

like image 45
driushkin Avatar answered Nov 13 '22 05:11

driushkin


when serializing, there is a nice way to override the property name:

public class CustomJsonWriter : JsonTextWriter
{
    public CustomJsonWriter(TextWriter writer) : base(writer)
    {
    }

    public override void WritePropertyName(string name, bool escape)
    {
        if (name == "$type") name = "__type";
        base.WritePropertyName(name, escape);
    }
}

var serializer = new JsonSerializer();
var writer = new StreamWriter(stream) { AutoFlush = true };
serializer.Serialize(new CustomJsonWriter(writer), objectToSerialize);

I haven't tried deserialization yet, but in worst case I could use:

json.Replace("\"__type": \"", "\"type\": \"$type\");
like image 34
Liero Avatar answered Nov 13 '22 04:11

Liero