浏览代码

updated serialization

/main
Steven Leal 4 年前
当前提交
59045e24
共有 2 个文件被更改,包括 77 次插入25 次删除
  1. 34
      com.unity.perception/Runtime/Randomization/Scenarios/Serialization/JsonConverters.cs
  2. 68
      com.unity.perception/Runtime/Randomization/Scenarios/Serialization/ScenarioTemplateSerializer.cs

34
com.unity.perception/Runtime/Randomization/Scenarios/Serialization/JsonConverters.cs


{
public class GroupItemsConverter : JsonConverter
{
public override bool CanWrite => false;
public override bool CanWrite => true;
public override bool CanRead => true;

public override void WriteJson(
JsonWriter writer, object value, JsonSerializer serializer)
{
throw new InvalidOperationException("Use default serialization.");
var output = new JObject();
var groupItems = (Dictionary<string, IGroupItem>)value;
foreach (var itemKey in groupItems.Keys)
{
var itemValue = groupItems[itemKey];
var newObj = new JObject();
if (itemValue is Parameter)
newObj["param"] = JObject.FromObject(itemValue);
else
newObj["scalar"] = JObject.FromObject(itemValue);
output[itemKey] = newObj;
}
output.WriteTo(writer);
}
public override object ReadJson(

public class ParameterItemsConverter : JsonConverter
{
public override bool CanWrite => false;
public override bool CanRead => true;
public override bool CanRead => true;
public override bool CanWrite => true;
public override bool CanConvert(Type objectType)
{

public override void WriteJson(
JsonWriter writer, object value, JsonSerializer serializer)
{
throw new InvalidOperationException("Use default serialization.");
var output = new JObject();
var parameterItems = (Dictionary<string, IParameterItem>)value;
foreach (var itemKey in parameterItems.Keys)
{
var itemValue = parameterItems[itemKey];
var newObj = new JObject();
if (itemValue is SamplerOptions)
newObj["samplerOptions"] = JObject.FromObject(itemValue);
else
newObj["scalar"] = JObject.FromObject(itemValue);
output[itemKey] = newObj;
}
output.WriteTo(writer);
}
public override object ReadJson(

68
com.unity.perception/Runtime/Randomization/Scenarios/Serialization/ScenarioTemplateSerializer.cs


using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Newtonsoft.Json;
using UnityEditor;
using UnityEngine.Perception.Randomization.Parameters;

static Group SerializeRandomizer(Randomizer randomizer)
{
var randomizerObj = new Group();
var parameterFields = randomizer.GetType().GetFields();
foreach (var parameterField in parameterFields)
var randomizerData = new Group();
var fields = randomizer.GetType().GetFields();
foreach (var field in fields)
if (!IsSubclassOfRawGeneric(typeof(NumericParameter<>), parameterField.FieldType))
continue;
var parameter = (Randomization.Parameters.Parameter)parameterField.GetValue(randomizer);
var parameterData = SerializeParameter(parameter);
if (parameterData.items.Count == 0)
continue;
randomizerObj.items.Add(parameterField.Name, parameterData);
if (field.FieldType.IsSubclassOf(typeof(Randomization.Parameters.Parameter)))
{
if (!IsSubclassOfRawGeneric(typeof(NumericParameter<>), field.FieldType))
continue;
var parameter = (Randomization.Parameters.Parameter)field.GetValue(randomizer);
var parameterData = SerializeParameter(parameter);
if (parameterData.items.Count == 0)
continue;
randomizerData.items.Add(field.Name, parameterData);
}
else
{
var scalarValue = ScalarFromField(field, randomizer);
if (scalarValue != null)
randomizerData.items.Add(field.Name, new Scalar { value = scalarValue });
}
return randomizerObj;
return randomizerData;
var samplerFields = parameter.GetType().GetFields();
foreach (var samplerField in samplerFields)
var fields = parameter.GetType().GetFields();
foreach (var field in fields)
if (!samplerField.FieldType.IsAssignableFrom(typeof(ISampler)))
continue;
var sampler = (ISampler)samplerField.GetValue(parameter);
var samplerData = SerializeSampler(sampler);
if (samplerData.defaultSampler == null)
continue;
parameterData.items.Add(samplerField.Name, samplerData);
if (field.FieldType.IsAssignableFrom(typeof(ISampler)))
{
var sampler = (ISampler)field.GetValue(parameter);
var samplerData = SerializeSampler(sampler);
if (samplerData.defaultSampler == null)
continue;
parameterData.items.Add(field.Name, samplerData);
}
else
{
var scalarValue = ScalarFromField(field, parameter);
if (scalarValue != null)
parameterData.items.Add(field.Name, new Scalar { value = scalarValue });
}
}
return parameterData;
}

else
throw new ArgumentException($"Invalid sampler type ({sampler.GetType()})");
return samplerData;
}
static IScalarValue ScalarFromField(FieldInfo field, object obj)
{
if (field.FieldType == typeof(string))
return new StringScalarValue { str = (string)field.GetValue(obj) };
if (field.FieldType == typeof(bool))
return new BooleanScalarValue { boolean = (bool)field.GetValue(obj) };
if (field.FieldType == typeof(float) || field.FieldType == typeof(double) || field.FieldType == typeof(int))
return new DoubleScalarValue { num = Convert.ToDouble(field.GetValue(obj)) };
return null;
}
static bool IsSubclassOfRawGeneric(Type generic, Type toCheck) {

正在加载...
取消
保存