0

Suppose I have the following classes:

public class SomeBase
{
}

public class ACommon : SomeBase
{
    [IsInput, IsOutput, IsInternal]
    public int AInput1 { get; set; }
    [IsOutput, IsInternal]
    public int AOutput1 { get; set; }
    [IsInternal]
    public int AInternal1 { get; set; }

    [IsOutput, IsInternal]
    public new BCommon ARefToB { get; set; }
}


public class BCommon : SomeBase
{
    [IsInput, IsOutput, IsInternal]
    public int BInput1 { get; set; }
    [IsOutput, IsInternal]
    public int BOutput1 { get; set; }
    [IsInternal]
    public int BInternal1 { get; set; }
}

Let's say I have the following JSON describing an ACommon object:

"{\"AInput1\":1,\"AOutput1\":2,\"AInternal1\":3,\"ARefToB\":{\"BInput1\":11,\"BOutput1\":12,\"BInternal1\":13}}"

I would like to be able to deserialize this string 3 different ways:

  1. All properties are deserialized
  2. All properties with IsInternal attribute are set to some custom value
  3. All properties with IsInternal or IsOutput attribute are set to some custom value

So, for example, when I deserialize with the third option, my objects would look like:

A.AInput = 1

A.AOutput1 = -1

A.AInternal1 = -1

A.ARefToB.BInput1 = 11

A.ARefToB.BOutput1 = -1

A.ARefToB.BInternal1 = -1

I've been looking into using a custom converter, but haven't had success yet in recursively converting every object within the parent object in the same way.

sbenderli
  • 3,654
  • 9
  • 35
  • 48
  • 1
    I'm pretty sure you will have to implement a custom converter. Have you seen this question? http://stackoverflow.com/questions/8030538/how-to-implement-custom-jsonconverter-in-json-net-to-deserialize-a-list-of-base Also, if you can provide the code for a nearly functional custom converter I'll revisit this and try to provide whatever fixes are necessary. – evanmcdonnal Jan 16 '14 at 19:18
  • @evanmcdonnal I've seen this question, but it doesn't help me, because it doesn't provide a way to look into property attributes, and set them accordingly. – sbenderli Jan 16 '14 at 19:22
  • Have you seen the docs on serialization attributes? My work with them has been pretty minimal but the docs are a good starting point since they at least will give you info on how to access those values; http://james.newtonking.com/json/help/index.html?topic=html/SerializationAttributes.htm – evanmcdonnal Jan 16 '14 at 19:24

0 Answers0