Custom serializer for only one property in Json.NET

UPDATE Found a problem - inherited from the wrong class, must be JsonConverter.

I have a class that has a Location property of type System.Data.Entity.Spatial.DbGeography. The Json.NET serializer, by default, displays JSON text as follows:

... "PlaceType": 0, "Location": { "Geography": { "CoordinateSystemId": 4326, "WellKnownText": "POINT (-88.00000 44.00000)" } }, "AddedDT": null, ... 

I want it to look like this:

  ... "PlaceType": 0, "Location": [-88.00000,44.00000], "AddedDT": null, ... 

... so it seems to me what I should do is to override any converter currently used for the DbGeography type.

The examples I've seen so far that use CustomCreationConverters and ContractResolvers seem to relate to how you replace the serializer for the serializable main class, and not for the type that is only a property of this class. Examples that include annotating a class that is being redefined do not work for me because I do not define DbGeography in my code, and this is actually a private class because it does not have a constructor and can only be created by internal factory methods.

Is there a way to apply JsonConverter to a type freely? If so, what would the converter look like? Should I just override the WriteJson () method?

+13
source share
3 answers

Turns out I just needed to inherit from JsonConverter instead of CustomCreationConverter, and everything else I tried to change was fine all the time.

I'm still not sure if there is a way to apply JsonConverter smoothly, but there is another way to use JsonConverter without referencing Json.NET in your domain / base project or marking your domain classes with links to peripherals library:

 var jsonSerializer = new JsonSerializer(); jsonSerializer.Converters.Add(new DbGeographyConverter()); jsonSerializer.Serialize(jsonWriter, place); 
+5
source

You can add your own serializer to a single attribute, like this:

 public class Comment { public string Author { get; set; } [JsonConverter(typeof(NiceDateConverter))] public DateTime Date { get; set; } public string Text { get; set; } } public class NiceDateConverter : JsonConverter { public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { var date = value as DateTime; var niceLookingDate = date.ToString("MMMM dd, yyyy 'at' H:mm tt"); writer.WriteValue(niceLookingDate); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { throw new NotImplementedException("Unnecessary because CanRead is false. The type will skip the converter."); } public override bool CanRead { get { return false; } } public override bool CanConvert(Type objectType) { return objectType == typeof(DateTime); } } 

Then, when you serialize your object using JsonConvert.SerializeObject (), a custom serializer will be used for the Date property.

+19
source

Use JsonConverterAttribute for the property and define a custom converter-

for example, we have a property that goes into the value of unix (long int), and we serialize it to .Net DateTime :

 [JsonConverter(typeof(UnixTimeJsonConverter))] public DateTime Requested { get; set; } 
+7
source

Source: https://habr.com/ru/post/952788/


All Articles