Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON.Net: Force serialization of all private fields and all fields in sub-classes

Tags:

c#

json.net

I have a class with several different classes and I send the information in these classes out to clients but I don't want to send them all out so some are private, some have the [JsonObject(MemberSerialization.OptIn)] flag etc.

However, now I want to do a backup of all these objects when I need to shutdown the server and every 12 hours (I don't want to use a database) so what I want to do (if possible) is to force the JSON.Net Serializer to convert the object and all the object belonging to that object.

For example:

class Foo
{
  public int Number;
  private string name;
  private PrivateObject po = new PrivateObject();

  public string ToJSON()
  { /* Serialize my public field, my property and the object PrivateObject */ }
}

I tried this code (even though it's obsolete) but it doesn't Serialize the objects related to my object:

Newtonsoft.Json.JsonSerializerSettings jss = new Newtonsoft.Json.JsonSerializerSettings();

Newtonsoft.Json.Serialization.DefaultContractResolver dcr = new Newtonsoft.Json.Serialization.DefaultContractResolver();
dcr.DefaultMembersSearchFlags |= System.Reflection.BindingFlags.NonPublic;
jss.ContractResolver = dcr;

return Newtonsoft.Json.JsonConvert.SerializeObject(this, jss);
like image 612
Westerlund.io Avatar asked Jun 08 '14 14:06

Westerlund.io


People also ask

Does JSON net serialize private fields?

All fields, both public and private, are serialized and properties are ignored. This can be specified by setting MemberSerialization. Fields on a type with the JsonObjectAttribute or by using the . NET SerializableAttribute and setting IgnoreSerializableAttribute on DefaultContractResolver to false.

Is JSON serialized or Deserialized?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object). If you serialize this result it will generate a text with the structure and the record returned.

What is JsonSerializerSettings?

Specifies the settings on a JsonSerializer object. Newtonsoft.Json. JsonSerializerSettings. Namespace: Newtonsoft.Json.

What is a JSON serialization exception?

JsonSerializationException(String, Exception) Initializes a new instance of the JsonSerializationException class with a specified error message and a reference to the inner exception that is the cause of this exception.


4 Answers

This should work:

var settings = new JsonSerializerSettings() { ContractResolver = new MyContractResolver() };
var json = JsonConvert.SerializeObject(obj, settings);

public class MyContractResolver : Newtonsoft.Json.Serialization.DefaultContractResolver
{
    protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
    {
        var props = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
                        .Select(p => base.CreateProperty(p, memberSerialization))
                    .Union(type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
                               .Select(f => base.CreateProperty(f, memberSerialization)))
                    .ToList();
        props.ForEach(p => { p.Writable = true; p.Readable = true; });
        return props;
    }
}
like image 64
L.B Avatar answered Sep 28 '22 11:09

L.B


@L.B's answer is great. But ... it requires .NET 3.5 or above.

For those of us stuck with 2.0 ...

public class ForceJSONSerializePrivatesResolver : Newtonsoft.Json.Serialization.DefaultContractResolver
{
    protected override IList<Newtonsoft.Json.Serialization.JsonProperty> CreateProperties(System.Type type, MemberSerialization memberSerialization)
    {
        var props = type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

        List<Newtonsoft.Json.Serialization.JsonProperty> jsonProps = new List<Newtonsoft.Json.Serialization.JsonProperty>();

        foreach( var prop in props )
        {
        jsonProps.Add( base.CreateProperty(prop, memberSerialization));
        }

        foreach( var field in type.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance) )
        {
        jsonProps.Add ( base.CreateProperty( field, memberSerialization ) );
        }

        jsonProps.ForEach(p => { p.Writable = true; p.Readable = true; });
        return jsonProps;
    }
}

...seems to work.

like image 34
Adam Avatar answered Sep 28 '22 10:09

Adam


Awesome thanks @L.B. Here's a full implementation in a .linq script in case anyone wants to test with private subclasses - e.g. See A has private subclass B.

void Main()
{
    var a = A.Test();
    SerialiseAllFields.Dump(a);
}

class A
{
    private int PrivField1;
    private int PrivProp1 { get; set; }
    private B PrivSubClassField1;

    public static A Test()
    {
        return new A { PrivField1 = 1, PrivProp1 = 2, PrivSubClassField1 = B.Test() };
    }
}

class B
{
    private int PrivField1;
    private int PrivProp1 { get; set; }

    public static B Test()
    {
        return new B { PrivField1 = 3, PrivProp1 = 4 };
    }
}

// Define other methods and classes here
public static class SerialiseAllFields
{
    public static void Dump(object o, bool indented = true)
    {
        var settings = new Newtonsoft.Json.JsonSerializerSettings() { ContractResolver = new AllFieldsContractResolver() };
        if (indented)
        {
            settings.Formatting = Newtonsoft.Json.Formatting.Indented;
        }
        Newtonsoft.Json.JsonConvert.SerializeObject(o, settings).Dump();
    }
}

public class AllFieldsContractResolver : Newtonsoft.Json.Serialization.DefaultContractResolver
{
    protected override IList<Newtonsoft.Json.Serialization.JsonProperty> CreateProperties(Type type, Newtonsoft.Json.MemberSerialization memberSerialization)
    {
        var props = type
            .GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
            .Select(p => base.CreateProperty(p, memberSerialization))
            .Union(type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
            .Select(f => base.CreateProperty(f, memberSerialization)))
            .ToList();
        props.ForEach(p => { p.Writable = true; p.Readable = true; });
        return props;
    }
}

The interesting thing is that the backing fields for the properties are also serialized i.e. output is:

{
  "PrivProp1": 2,
  "PrivField1": 1,
  "<PrivProp1>k__BackingField": 2,
  "PrivSubClassField1": {
    "PrivProp1": 4,
    "PrivField1": 3,
    "<PrivProp1>k__BackingField": 4
  }
}
like image 43
Ilan Avatar answered Sep 28 '22 12:09

Ilan


this is modified version of the previous accepted answer, this will also serialize private fields/properties. performance a bit increased. (serialization without BinaryFormatter a bit slower)

public class CloneContractResolver : Newtonsoft.Json.Serialization.DefaultContractResolver
{
    protected override IList<JsonProperty> CreateProperties(Type type,
                                MemberSerialization memberSerialization)
    {
        List<MemberInfo> members = GetSerializableMembers(type);
        if (members == null)
         throw new JsonSerializationException("Null collection of serializable members returned.");

        members.AddRange(type.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance)
            .Where(f => !f.CustomAttributes.Any(x => x.AttributeType
                == typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute))));

        members.AddRange(type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
            .Where(f => !f.CustomAttributes.Any(x => x.AttributeType
                == typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute))));

        JsonPropertyCollection properties = new JsonPropertyCollection(type);
        members.ForEach(member =>
        {
            JsonProperty property = CreateProperty(member, memberSerialization);
            property.Writable = true;
            property.Readable = true;
            properties.AddProperty(property);
        });
        return properties;
    }
}

in my opinion, this method will use a bit more memory

public static class CloneHelper
{
    private readonly static JsonSerializerSettings clone_settings = new JsonSerializerSettings() { ContractResolver = new CloneContractResolver() };
    public static T Clone<T>(this object source)
    {
        T entity = JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(source, clone_settings), clone_settings);
        return entity;
    }
}
like image 42
Mustafa Salih ASLIM Avatar answered Sep 28 '22 12:09

Mustafa Salih ASLIM