0

What is the simplest and elegant way to parse JSON with Json.Net and check if it is subtype? Here is an example.

Shared class library:

public abstract class State {}
public class PlayerState : State {}
public class VehicleState : State {}

Server-side:

 var _state = Utils.GetStateFromString<State>(s);
            if (_state is PlayerState) {
                PlayerState state = (PlayerState) _state;
                ...
            }
            else if (_state is VehicleState) {
                VehicleState state = (VehicleState) _state;
                 ...
            }

where s is serialized string from client.

I get it's works only using TypeNameHandling = TypeNameHandling.Objects and creating custom SerializationBinder, but it is very hart to add all types to binder, because of there are different assemblies on client and server. I know about JsonSubType lib, but my platform doesn't allow adding links in project

FoxPro
  • 2,054
  • 4
  • 11
  • 34
  • I cannot understand this code. In which class do you define `_state` and the following script? – 0009laH Oct 03 '19 at 14:07
  • What is in `s`? (hint: add it to your question). Add an example of the desired output... – Stuart Oct 03 '19 at 14:16
  • Possible duplicate of [Deserializing polymorphic json classes without type information using json.net](https://stackoverflow.com/q/19307752/10263) – Brian Rogers Oct 03 '19 at 14:44

0 Answers0