Skip to content
This repository has been archived by the owner on May 16, 2022. It is now read-only.

Commit

Permalink
support base class private member deserialize #43
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Jan 30, 2018
1 parent 9c62409 commit a9b7dc7
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 11 deletions.
24 changes: 16 additions & 8 deletions sandbox/ConsoleAppNetCore/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ public class FooException : Exception

namespace ConsoleAppNetCore
{
public abstract class JsonDBItem
{
public int ID { get { return _id; } }
private int _id = -1;
}

public class Card : JsonDBItem
{
public string Name { get { return name; } }
private string name = "0";
}


public class CustomPoint
{
public readonly int X;
Expand Down Expand Up @@ -340,15 +353,10 @@ public sealed class Entry

static unsafe void Main(string[] args)
{
var argNullEx = new ArgumentNullException("cooCoo", "parameter cannot be null");
var payload = new Entry
{
Number = 1,
Exception = argNullEx
};

var json1 = Utf8Json.JsonSerializer.ToJsonString(payload, StandardResolver.AllowPrivateCamelCase);
Console.WriteLine(JsonSerializer.PrettyPrint(json1));
var huga = JsonSerializer.Serialize<Card>(new Card());


}

static (int, int[]) Array(int[] xs)
Expand Down
5 changes: 2 additions & 3 deletions src/Utf8Json/Internal/Emit/MetaType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public MetaType(Type type, Func<string, string> nameMutetor, bool allowPrivate)
var stringMembers = new Dictionary<string, MetaMember>();

{
// All public members are serialize target except [Ignore] member.
foreach (var item in type.GetRuntimeProperties())
foreach (var item in type.GetAllProperties())
{
if (item.GetIndexParameters().Length > 0) continue; // skip indexer
if (item.GetCustomAttribute<IgnoreDataMemberAttribute>(true) != null) continue;
Expand All @@ -44,7 +43,7 @@ public MetaType(Type type, Func<string, string> nameMutetor, bool allowPrivate)
}
stringMembers.Add(member.Name, member);
}
foreach (var item in type.GetRuntimeFields())
foreach (var item in type.GetAllFields())
{
if (item.GetCustomAttribute<IgnoreDataMemberAttribute>(true) != null) continue;
if (item.GetCustomAttribute<System.Runtime.CompilerServices.CompilerGeneratedAttribute>(true) != null) continue;
Expand Down
46 changes: 46 additions & 0 deletions src/Utf8Json/Internal/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,52 @@ public static bool IsAnonymous(this System.Reflection.TypeInfo type)
&& (type.Attributes & TypeAttributes.NotPublic) == TypeAttributes.NotPublic;
}

public static IEnumerable<PropertyInfo> GetAllProperties(this Type type)
{
return GetAllPropertiesCore(type, new HashSet<string>());
}

static IEnumerable<PropertyInfo> GetAllPropertiesCore(Type type, HashSet<string> nameCheck)
{
foreach (var item in type.GetRuntimeProperties())
{
if (nameCheck.Add(item.Name))
{
yield return item;
}
}
if (type.BaseType != null)
{
foreach (var item in GetAllPropertiesCore(type.BaseType, nameCheck))
{
yield return item;
}
}
}

public static IEnumerable<FieldInfo> GetAllFields(this Type type)
{
return GetAllFieldsCore(type, new HashSet<string>());
}

static IEnumerable<FieldInfo> GetAllFieldsCore(Type type, HashSet<string> nameCheck)
{
foreach (var item in type.GetRuntimeFields())
{
if (nameCheck.Add(item.Name))
{
yield return item;
}
}
if (type.BaseType != null)
{
foreach (var item in GetAllFieldsCore(type.BaseType, nameCheck))
{
yield return item;
}
}
}

#if NETSTANDARD

public static bool IsConstructedGenericType(this System.Reflection.TypeInfo type)
Expand Down
40 changes: 40 additions & 0 deletions tests/Utf8Json.Tests/BassClassDeserializeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Text;
using Xunit;

namespace Utf8Json.Tests
{
public abstract class Base1
{
public int B1 { get { return _b1; } }
private int _b1 = -1;
}

public abstract class Base2 : Base1
{
public int B2 { get { return _b2; } }
private int _b2 = -2;
}

public class Impl1_2 : Base2
{
public string Name { get { return name; } }
private string name = "none";
}

public class BassClassDeserializeTest
{
[Fact]
public void Deserialize()
{
var json = "{\"_b1\":10,\"_b2\":99,\"name\":\"foobar\"}";

var r = JsonSerializer.Deserialize<Impl1_2>(json, Utf8Json.Resolvers.StandardResolver.AllowPrivate);

r.B1.Is(10);
r.B2.Is(99);
r.Name.Is("foobar");
}
}
}

0 comments on commit a9b7dc7

Please sign in to comment.