neuecc / Utf8Json

Definitely Fastest and Zero Allocation JSON Serializer for C#(NET, .NET Core, Unity, Xamarin).
MIT License
2.36k stars 267 forks source link

Question: How to decorate types on serialization with additional properties #117

Open Montrichard opened 5 years ago

Montrichard commented 5 years ago

Hi,

I currently use Newtonsoft and have my own ContractResolver that inherits from the DefaultContractResolver. My class overrides the CreateProperties method and when a type is passed to that method I call base.CreateProperties() which returns a List<JsonProperty> and I then add new properties to the list so that when it is serialized I have the extra properties eg.

public class Person
{
  public string Name {get;set;}
}
{ 
  "@id" : 1,
  "@type" : "foobar",
  "name" : "bruce wayne"
}

I have the README about json formatters and resolvers but I don't quite see how I can manipulate every object that is passed to the serializer, for example the formatters are based on type eg IJsonFormatter<T> but I'm not sure if implementing that as public class MyFormatter : IJsonFormatter<object> would be the correct way to go?

Could you offer some advice on how to manipulate the object before it's serialized?

Thanks