Skip to content

Commit

Permalink
#861 Add Doc marshalling with type
Browse files Browse the repository at this point in the history
  • Loading branch information
zhabis committed May 11, 2023
1 parent 7127c58 commit 518da8d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
4 changes: 1 addition & 3 deletions src/Azos/Apps/typeid/GuidTypeAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ public static TAttribute TryGetGuidTypeAttribute<TDecorationTarget, TAttribute>(
}

private static FiniteSetLookup<Type, FiniteSetLookup<Type, GuidTypeAttribute>> s_Cache =
new FiniteSetLookup<Type, FiniteSetLookup<Type, GuidTypeAttribute>>(
ttarget => new FiniteSetLookup<Type, GuidTypeAttribute>( tattr => ttarget.GetCustomAttribute(tattr, false) as GuidTypeAttribute)
);
new (ttarget => new FiniteSetLookup<Type, GuidTypeAttribute>( tattr => ttarget.GetCustomAttribute(tattr, false) as GuidTypeAttribute));


protected GuidTypeAttribute(string typeGuid)
Expand Down
45 changes: 35 additions & 10 deletions src/Azos/Serialization/Bix/Bixon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@ private static JsonDataArray readArchivedDataArray(BixReader reader, byte ver)
/// <summary>
/// Writes object: a primitive, anonymous object, JsonDataMap or JsonDataArray
/// </summary>
public static void WriteObject(BixWriter writer, object obj)
public static void WriteObject(BixWriter writer, object obj, JsonWritingOptions jopt = null)
{
writer.Write(HEADER1);
writer.Write(HEADER2);
writer.Write(VERSION);
writeValue(writer, obj, null);
writeValue(writer, obj, jopt, null);
}

private static void writeArchivedDataMap(BixWriter writer, JsonDataMap map, HashSet<IJsonDataObject> set)
private static void writeArchivedDataMap(BixWriter writer, JsonDataMap map, JsonWritingOptions jopt, HashSet<IJsonDataObject> set)
{
if (map == null)
{
Expand All @@ -148,12 +148,12 @@ private static void writeArchivedDataMap(BixWriter writer, JsonDataMap map, Hash
foreach(var kvp in map)
{
writer.Write(kvp.Key);//string property name
writeValue(writer, kvp.Value, set);
writeValue(writer, kvp.Value, jopt, set);
}
writer.Write((string)null);//eof
}

private static void writeArchivedDataArray(BixWriter writer, JsonDataArray array, HashSet<IJsonDataObject> set)
private static void writeArchivedDataArray(BixWriter writer, JsonDataArray array, JsonWritingOptions jopt, HashSet<IJsonDataObject> set)
{
if (array == null)
{
Expand All @@ -167,7 +167,7 @@ private static void writeArchivedDataArray(BixWriter writer, JsonDataArray array
writer.Write(array.Count);
for(var i=0; i< array.Count; i++)
{
writeValue(writer, array[i], set);
writeValue(writer, array[i], jopt, set);
}
}

Expand Down Expand Up @@ -221,16 +221,41 @@ private static void writeArchivedDataArray(BixWriter writer, JsonDataArray array
{TypeCode.Buffer, (r, ver) => r.ReadBuffer() },
};

private static void writeValue(BixWriter writer, object value, HashSet<IJsonDataObject> set)
private static void writeValue(BixWriter writer, object value, JsonWritingOptions jopt, HashSet<IJsonDataObject> set)
{
if (value == null)
{
writer.Write(TypeCode.Null);
return;
}

var tvalue = value.GetType();

//Reinterpret new{a=1} as JsonDataMap
if (value.GetType().IsAnonymousType()) value = anonymousToMap(value);
if (tvalue.IsAnonymousType()) value = anonymousToMap(value);
//Reinterpret Doc as map
else if (value is Doc doc)
{
var includeType = jopt != null && jopt.Purpose == JsonSerializationPurpose.Marshalling;

if (includeType)//include type code
{
writer.Write(TypeCode.DocWithType);
var atr = BixAttribute.TryGetGuidTypeAttribute<TypedDoc, BixAttribute>(tvalue);
if (atr != null)
{
writer.WriteFixedBE64bits(atr.TypeFguid.S1);
writer.WriteFixedBE64bits(atr.TypeFguid.S2);
}
else
{
for(var i=0; i<16; i++) writer.Write((byte)0);
}
}
value = doc.ToJsonDataMap(jopt);
}//doc


if (value is JsonDataMap map)
{
writer.Write(TypeCode.Map);
Expand All @@ -243,7 +268,7 @@ private static void writeValue(BixWriter writer, object value, HashSet<IJsonData
Aver.IsTrue(set.Add(map), "circular reference check");
try
{
writeArchivedDataMap(writer, map, set);
writeArchivedDataMap(writer, map, jopt, set);
}
finally
{
Expand All @@ -266,7 +291,7 @@ private static void writeValue(BixWriter writer, object value, HashSet<IJsonData
Aver.IsTrue(set.Add(array), "circular reference check");
try
{
writeArchivedDataArray(writer, array, set);
writeArchivedDataArray(writer, array, jopt, set);
}
finally
{
Expand Down

0 comments on commit 518da8d

Please sign in to comment.