4

I am converting some logic from Newtonsoft and found one of following implementation

 public class CustomDataContractResolver : DefaultContractResolver
    {
        public Dictionary<string, string> FieldNameChanges { get; set; }
        public List<FieldValueReplica> FieldValueReplica { get; set; }

        protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
        {
            var property = base.CreateProperty(member, memberSerialization);
            if (property.DeclaringType != typeof(logEvent)) return property;

            if (FieldNameChanges.Count > 0 && FieldNameChanges.TryGetValue(property.PropertyName, out var newValue))
                property.PropertyName = newValue;
            
            return property;
        }
   }

Any one have converted DefaultContractResolver to System.Text.Json

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
Kamran Shahid
  • 3,954
  • 5
  • 48
  • 93
  • 1
    There is none that is public. See: [System.Text.Json API is there something like IContractResolver](https://stackoverflow.com/q/58926112/3744182). – dbc Nov 07 '20 at 15:42
  • :( Thanks dbc. Then could anyone help in above logic conversion to System.Text.Json – Kamran Shahid Nov 07 '20 at 16:02
  • You would need to create a custom [`JsonConverter`](https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-converters-how-to) for `logEvent`. Alternatively, the linked question has an answer that suggests some third party extension that might do the job, but I haven't tried it so I can't say one way or the other. – dbc Nov 07 '20 at 16:16
  • no problem dbc. i am checking that link. may be some other guy have done similar and help in it – Kamran Shahid Nov 07 '20 at 20:00
  • .NET 7 now provides custom contract resolvers. See [this answer](https://stackoverflow.com/a/74284215/3744182) to [System.Text.Json API is there something like IContractResolver](https://stackoverflow.com/q/58926112/3744182). Does that answer your question, or do you need a specific answer? – dbc Nov 12 '22 at 00:35

0 Answers0