浏览代码

fixed up converters

/main
Steven Leal 4 年前
当前提交
f13b8158
共有 7 个文件被更改,包括 279 次插入164 次删除
  1. 3
      com.unity.perception/Runtime/Randomization/Scenarios/Serialization.meta
  2. 164
      com.unity.perception/Runtime/Randomization/Scenarios/Serialization/JsonConverters.cs
  3. 3
      com.unity.perception/Runtime/Randomization/Scenarios/Serialization/JsonConverters.cs.meta
  4. 109
      com.unity.perception/Runtime/Randomization/Scenarios/Serialization/ScenarioSerialization.cs
  5. 164
      com.unity.perception/Runtime/Randomization/Scenarios/ScenarioSerialization.cs
  6. 0
      /com.unity.perception/Runtime/Randomization/Scenarios/Serialization/ScenarioSerialization.cs.meta

3
com.unity.perception/Runtime/Randomization/Scenarios/Serialization.meta


fileFormatVersion: 2
guid: 16628d9798ec4551b9648c56ef672ecc
timeCreated: 1612905161

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


using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace UnityEngine.Perception.Randomization.Scenarios.Serialization
{
public class GroupItemsConverter : JsonConverter
{
public override bool CanWrite => false;
public override bool CanRead => true;
public override bool CanConvert(Type objectType)
{
return objectType == typeof(IGroupItem);
}
public override void WriteJson(
JsonWriter writer, object value, JsonSerializer serializer)
{
throw new InvalidOperationException("Use default serialization.");
}
public override object ReadJson(
JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var jsonObject = JObject.Load(reader);
var groupItems = new Dictionary<string, IGroupItem>();
foreach (var property in jsonObject.Properties())
{
var value = (JObject)property.Value;
IGroupItem groupItem;
if (value.ContainsKey("param"))
groupItem = serializer.Deserialize<Parameter>(value["param"].CreateReader());
else if (value.ContainsKey("scalar"))
groupItem = serializer.Deserialize<Scalar>(value["scalar"].CreateReader());
else
throw new KeyNotFoundException("No GroupItem key found");
groupItems.Add(property.Name, groupItem);
}
return groupItems;
}
}
public class ParameterItemsConverter : JsonConverter
{
public override bool CanWrite => false;
public override bool CanRead => true;
public override bool CanConvert(Type objectType)
{
return objectType == typeof(IParameterItem);
}
public override void WriteJson(
JsonWriter writer, object value, JsonSerializer serializer)
{
throw new InvalidOperationException("Use default serialization.");
}
public override object ReadJson(
JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var jsonObject = JObject.Load(reader);
var parameterItems = new Dictionary<string, IParameterItem>();
foreach (var property in jsonObject.Properties())
{
var value = (JObject)property.Value;
IParameterItem parameterItem;
if (value.ContainsKey("samplerOptions"))
parameterItem = serializer.Deserialize<SamplerOptions>(value["samplerOptions"].CreateReader());
else if (value.ContainsKey("scalar"))
parameterItem = serializer.Deserialize<Scalar>(value["scalar"].CreateReader());
else
throw new KeyNotFoundException("No ParameterItem key found");
parameterItems.Add(property.Name, parameterItem);
}
return parameterItems;
}
}
public class SamplerOptionsConverter : JsonConverter
{
public override bool CanRead => true;
public override bool CanWrite => true;
public override bool CanConvert(Type objectType)
{
return objectType == typeof(SamplerOptions);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var options = (SamplerOptions)value;
var output = new JObject { ["metadata"] = JObject.FromObject(options.metadata) };
string key;
if (options.defaultSampler is ConstantSampler)
key = "constant";
else if (options.defaultSampler is UniformSampler)
key = "uniform";
else if (options.defaultSampler is NormalSampler)
key = "normal";
else
throw new TypeAccessException($"Cannot serialize type ${options.defaultSampler.GetType()}");
output[key] = JObject.FromObject(options.defaultSampler);
output.WriteTo(writer);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var jsonObject = JObject.Load(reader);
var samplerOption = new SamplerOptions { metadata = jsonObject["metadata"].ToObject<StandardMetadata>() };
if (jsonObject.ContainsKey("constant"))
samplerOption.defaultSampler = jsonObject["constant"].ToObject<ConstantSampler>();
else if (jsonObject.ContainsKey("uniform"))
samplerOption.defaultSampler = jsonObject["uniform"].ToObject<UniformSampler>();
else if (jsonObject.ContainsKey("normal"))
samplerOption.defaultSampler = jsonObject["normal"].ToObject<NormalSampler>();
else
throw new KeyNotFoundException("No valid SamplerOption key type found");
return samplerOption;
}
}
public class ScalarConverter : JsonConverter
{
public override bool CanRead => true;
public override bool CanWrite => false;
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Scalar);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var jsonObject = JObject.Load(reader);
var value = (JObject)jsonObject["value"];
var scalar = new Scalar { metadata = jsonObject["metadata"].ToObject<StandardMetadata>() };
if (value.ContainsKey("str"))
scalar.value = new StringScalarValue { str = value["str"].Value<string>() };
else if (value.ContainsKey("num"))
scalar.value = new DoubleScalarValue { num = value["num"].Value<double>() };
else if (value.ContainsKey("bool"))
scalar.value = new BooleanScalarValue { boolean = value["bool"].Value<bool>() };
else
throw new KeyNotFoundException("No valid ScalarValue key type found");
return scalar;
}
}
}

3
com.unity.perception/Runtime/Randomization/Scenarios/Serialization/JsonConverters.cs.meta


fileFormatVersion: 2
guid: f2954e9f75904733971e6c6ab1f275d8
timeCreated: 1612905182

109
com.unity.perception/Runtime/Randomization/Scenarios/Serialization/ScenarioSerialization.cs


using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using UnityEditor;
namespace UnityEngine.Perception.Randomization.Scenarios.Serialization
{
public static class ScenarioSerialization
{
[MenuItem("Tests/Deserialize Test")]
public static void DeserializeTest()
{
var jsonString = File.ReadAllText($"{Application.streamingAssetsPath}/data.json");
var schema = JsonConvert.DeserializeObject<TemplateConfigurationOptions>(jsonString);
var backToJson = JsonConvert.SerializeObject(schema, Formatting.Indented);
Debug.Log(backToJson);
}
}
#region Interfaces
public interface IGroupItem { }
public interface IParameterItem { }
public interface ISamplerOption { }
public interface IScalarValue { }
#endregion
#region GroupedObjects
public class TemplateConfigurationOptions
{
public Dictionary<string, Group> groups;
}
public class StandardMetadata
{
public string name = string.Empty;
public string description = string.Empty;
}
public class Group
{
public StandardMetadata metadata;
[JsonConverter(typeof(GroupItemsConverter))]
public Dictionary<string, IGroupItem> items;
}
public class Parameter : IGroupItem
{
public StandardMetadata metadata;
[JsonConverter(typeof(ParameterItemsConverter))]
public Dictionary<string, IParameterItem> items;
}
#endregion
#region SamplerOptions
[JsonConverter(typeof(SamplerOptionsConverter))]
public class SamplerOptions : IParameterItem
{
public StandardMetadata metadata;
public ISamplerOption defaultSampler;
}
public class UniformSampler : ISamplerOption
{
public double min;
public double max;
}
public class NormalSampler : ISamplerOption
{
public double min;
public double max;
public double mean;
public double standardDeviation;
}
public class ConstantSampler : ISamplerOption
{
public double value;
}
#endregion
#region ScalarValues
[JsonConverter(typeof(ScalarConverter))]
public class Scalar : IGroupItem, IParameterItem
{
public StandardMetadata metadata;
public IScalarValue value;
}
public class StringScalarValue : IScalarValue
{
public string str;
}
public class DoubleScalarValue : IScalarValue
{
public double num;
}
public class BooleanScalarValue : IScalarValue
{
public bool boolean;
}
#endregion
}

164
com.unity.perception/Runtime/Randomization/Scenarios/ScenarioSerialization.cs


using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityEditor;
namespace UnityEngine.Perception.Randomization.Scenarios.Serialization
{
public static class ScenarioSerialization
{
[MenuItem("Tests/Deserialize Test")]
public static void DeserializeTest()
{
var jsonString = File.ReadAllText($"{Application.streamingAssetsPath}/data.json");
var schema = JsonConvert.DeserializeObject<IncomingSchema>(jsonString);
}
}
public class IncomingSchema
{
public Dictionary<string, Group> groups;
}
public class MetaData
{
public string name = string.Empty;
public string description = string.Empty;
}
public class Group
{
public MetaData metadata;
[JsonConverter(typeof(GroupItemsConverter))]
public Dictionary<string, IGroupItem> items;
}
public interface IGroupItem { }
public class GroupItemsConverter : JsonConverter
{
public override bool CanWrite => false;
public override bool CanRead => true;
public override bool CanConvert(Type objectType)
{
return objectType == typeof(IGroupItem);
}
public override void WriteJson(
JsonWriter writer, object value, JsonSerializer serializer)
{
throw new InvalidOperationException("Use default serialization.");
}
public override object ReadJson(
JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var jsonObject = JObject.Load(reader);
var groupItems = new Dictionary<string, IGroupItem>();
foreach (var property in jsonObject.Properties())
{
var value = (JObject)property.Value;
var groupItem = value.ContainsKey("items") ? (IGroupItem)new Parameter() : new Scalar();
serializer.Populate(value.CreateReader(), groupItem);
groupItems.Add(property.Name, groupItem);
}
return groupItems;
}
}
public interface IParameterItem { }
public class ParameterItemsConverter : JsonConverter
{
public override bool CanWrite => false;
public override bool CanRead => true;
public override bool CanConvert(Type objectType)
{
return objectType == typeof(IGroupItem);
}
public override void WriteJson(
JsonWriter writer, object value, JsonSerializer serializer)
{
throw new InvalidOperationException("Use default serialization.");
}
public override object ReadJson(
JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var jsonObject = JObject.Load(reader);
var groupItems = new Dictionary<string, IGroupItem>();
foreach (var property in jsonObject.Properties())
{
var value = (JObject)property.Value;
var groupItem = value.ContainsKey("items") ? (IGroupItem)new SamplerOptions() : new Scalar();
serializer.Populate(value.CreateReader(), groupItem);
groupItems.Add(property.Name, groupItem);
}
return groupItems;
}
}
public class Parameter : IGroupItem
{
public MetaData metadata;
public Dictionary<string, IParameterItem> items;
}
public interface IScalarType { }
public class Scalar : IGroupItem, IParameterItem
{
public MetaData metadata;
public IScalarType scalarData;
}
public class StringScalarType : IScalarType
{
public string value;
}
public class DoubleScalarType : IScalarType
{
public double value;
}
public class BooleanScalarType : IScalarType
{
public bool value;
}
public interface ISamplerOption { }
public class SamplerOptions : IParameterItem
{
public MetaData metadata;
public List<ISamplerOption> options;
}
public class UniformSampler : ISamplerOption
{
public double min;
public double max;
}
public class NormalSampler : ISamplerOption
{
public double min;
public double max;
public double mean;
public double standardDeviation;
}
public class ConstantSampler : ISamplerOption
{
public float value;
}
}

/com.unity.perception/Runtime/Randomization/Scenarios/ScenarioSerialization.cs.meta → /com.unity.perception/Runtime/Randomization/Scenarios/Serialization/ScenarioSerialization.cs.meta

正在加载...
取消
保存