0

Description of the issue:

I want to validate the relation between classes in deserialized JSON object on the server. as an example: Claimed foreign key is correct between class A and class B.

A:

{
"Id": "z",
"B": [
    {
        "Id": "x",
    },
    {
        "Id": "y",
    }
   ]
}

In this case i want to figure out whether class A with Id "Z", has a valid relation to Class B with id "x" and "y" , And this can be deeper and deeper ... so B can have several inner relation and must be checked after B has been completely deserialized

I Don't want to write a recursive function, because i think it has lot of performance penalty for a server side program.

Some of property checks can be easily done using Newtonsoft.Json.Serialization.IValueProvider but the problem is, i can't find out when a class has been completely deserialized ( all required properties has their own value in last public void SetValue (object target, object value) call , some of them may have nothing at all and this feature is different for each class)

This part of server has been fully written with reflection and its completely generic.

So the question is: How can i find out this SetValue is the last SetValue call for this object? (for example object key is always the last call or something like it)

Or any other way for me to check this relation before IdentityDbContext.Update

Thanks

Mohammad
  • 75
  • 1
  • 11
  • You don't want to write a recursive function because of performance concerns but have no problem using reflection? – Abion47 Aug 08 '18 at 15:33
  • 1
    are you looking for the `[OnDeserialized]` callback? https://www.newtonsoft.com/json/help/html/SerializationCallbacks.htm – Marc Gravell Aug 08 '18 at 15:35
  • @Abion47 it’s about how to write a general purpose server with minimum penalty – Mohammad Aug 08 '18 at 15:49
  • @MarcGravell any other way with out modifying models ? – Mohammad Aug 08 '18 at 15:52
  • Recursion isn't inherently any less performant than plain old iteration. If it's the right tool for the job, use it. – Abion47 Aug 08 '18 at 15:55
  • 1
    Don't make implementation decisions based on uninformed guesses of how it will perform. Instead, write your implementation in the way that makes the most sense, then *measure* the performance to see if it is acceptable. If it is not, *then* work on improving it. – Brian Rogers Aug 08 '18 at 16:22
  • 1) If you are concerned about the performance of recursion, you could use as explicit stack & graph coloring algorithm such as the one shown in [this answer](https://stackoverflow.com/a/29707338/3744182) to [In C#, how to find chain of circular dependency?](https://stackoverflow.com/q/29703972/3744182). However, recursion is only problematic for *deep* trees; does that apply here? – dbc Aug 08 '18 at 21:43
  • 2) If you don't want to modify your models you could add a callback to [`JsonContract.OnDeserializedCallbacks`](https://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_Serialization_JsonContract_OnDeserializedCallbacks.htm) with a [custom contract resolver](https://www.newtonsoft.com/json/help/html/contractresolver.htm#CustomIContractResolverExamples). – dbc Aug 08 '18 at 21:43

0 Answers0