Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions src/benchmarks/micro/Serializers/DataGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ internal static T Generate<T>()
return (T)(object)new ArrayList(ValuesGenerator.ArrayOfUniqueValues<string>(100));
if (typeof(T) == typeof(Hashtable))
return (T)(object)new Hashtable(ValuesGenerator.ArrayOfUniqueValues<string>(100).ToDictionary(value => value));

if (typeof(T) == typeof(LargeStructWithProperties))
return (T)(object)CreateLargeStructWithProperties();
if (typeof(T) == typeof(int))
return (T)(object)42;

throw new NotImplementedException();
}
Expand Down Expand Up @@ -106,6 +109,16 @@ private static IndexViewModel CreateIndexViewModel()
count: 20).ToList()
};

private static LargeStructWithProperties CreateLargeStructWithProperties()
=> new LargeStructWithProperties
{
String1 = "1",
String2 = "2",
String3 = "3",
String4 = "4",
String5 = "5",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no initialization of the int properties?

};

private static MyEventsListerViewModel CreateMyEventsListerViewModel()
=> new MyEventsListerViewModel
{
Expand Down Expand Up @@ -357,7 +370,24 @@ public class CollectionsOfPrimitives

[ProtoMember(4)] [Key(3)] public List<int> ListOfInt { get; set; }
}


[Serializable]
[ProtoContract]
[MessagePackObject]
public struct LargeStructWithProperties
{
[ProtoMember(1)] [Key(0)] public string String1 { get; set; }
[ProtoMember(2)] [Key(1)] public string String2 { get; set; }
[ProtoMember(3)] [Key(2)] public string String3 { get; set; }
[ProtoMember(4)] [Key(3)] public string String4 { get; set; }
[ProtoMember(5)] [Key(4)] public string String5 { get; set; }
[ProtoMember(6)] [Key(5)] public int Int1 { get; set; }
[ProtoMember(7)] [Key(6)] public int Int2 { get; set; }
[ProtoMember(8)] [Key(7)] public int Int3 { get; set; }
[ProtoMember(9)] [Key(8)] public int Int4 { get; set; }
[ProtoMember(10)] [Key(9)] public int Int5 { get; set; }
}

public struct SimpleStructWithProperties
{
public int Num { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ namespace System.Text.Json.Serialization.Tests
[GenericTypeArguments(typeof(HashSet<string>))]
[GenericTypeArguments(typeof(ArrayList))]
[GenericTypeArguments(typeof(Hashtable))]
[GenericTypeArguments(typeof(SimpleStructWithProperties))]
[GenericTypeArguments(typeof(LargeStructWithProperties))]
[GenericTypeArguments(typeof(int))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the code looks good to me, but I just wonder if it is a real use case: (de)serializing a single integer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is there primarily to detect general front-end overhead of the serializer without any extra baggage.

However, one potential scenario is enumerating over a list of elements and calling serialize on each one.

However I don't recommend re-entering the serializer for each element -- first there is overhead of a dictionary lookup in the front-end to get the converter for each element and second because currently the "JsonPath" exception functionality doesn't work on re-entry (we need to add a new API to pass that state).

The better way is to write a custom converter for that collection which in turn would forward to a cached element converter. However, it may be that the list of element types are not known (base type of System.Object for example), so the dictionary lookup may need to occur per element anyway in order to find the converter for each element.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the explanation. If this provides value to you then I am merging it.

public class ReadJson<T>
{
private string _serialized;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ namespace System.Text.Json.Serialization.Tests
[GenericTypeArguments(typeof(HashSet<string>))]
[GenericTypeArguments(typeof(ArrayList))]
[GenericTypeArguments(typeof(Hashtable))]
[GenericTypeArguments(typeof(SimpleStructWithProperties))]
[GenericTypeArguments(typeof(LargeStructWithProperties))]
[GenericTypeArguments(typeof(int))]
public class WriteJson<T>
{
private T _value;
Expand Down