From 59be90f69199a72ec2580cb3eedf41ed033fd043 Mon Sep 17 00:00:00 2001 From: Martins Mozeiko Date: Tue, 12 Mar 2019 19:11:20 -0700 Subject: [PATCH] Clean up json/hdmap serialization code. --- Assets/Scripts/Map/MapUtil.cs | 141 +++++++------------------------- Assets/Scripts/Ros/RosBridge.cs | 103 +++++++---------------- Assets/Scripts/RosWriter.cs | 6 -- 3 files changed, 58 insertions(+), 192 deletions(-) diff --git a/Assets/Scripts/Map/MapUtil.cs b/Assets/Scripts/Map/MapUtil.cs index 48fd8e48..9f059d9b 100644 --- a/Assets/Scripts/Map/MapUtil.cs +++ b/Assets/Scripts/Map/MapUtil.cs @@ -490,7 +490,7 @@ public static Vector3 GetUnityPosition(Ros.PointENU point) public static void SerializeHDMap(HDMap map, out StringBuilder sb) { sb = new StringBuilder(); - SerializeInternal(1, sb, map.GetType(), map, sType: SerialType.HDMap); + SerializeInternal(1, sb, map.GetType(), map); sb.Trim(); if (sb[0] == '{') { @@ -502,35 +502,21 @@ public static void SerializeHDMap(HDMap map, out StringBuilder sb) } } - public enum SerialType - { - JSON, - HDMap, - } - static readonly Dictionary BuiltinMessageTypes = new Dictionary { - { typeof(bool), "std_msgs/Bool" }, - { typeof(sbyte), "std_msgs/Int8" }, - { typeof(short), "std_msgs/Int16" }, - { typeof(int), "std_msgs/Int32" }, - { typeof(long), "std_msgs/Int64" }, - { typeof(byte), "std_msgs/UInt8" }, - { typeof(ushort), "std_msgs/UInt16" }, - { typeof(uint), "std_msgs/UInt32" }, - { typeof(ulong), "std_msgs/UInt64" }, - { typeof(float), "std_msgs/Float32" }, - { typeof(double), "std_msgs/Float64" }, - { typeof(string), "std_msgs/String" }, + { typeof(bool), "std_msgs/Bool" }, + { typeof(sbyte), "std_msgs/Int8" }, + { typeof(short), "std_msgs/Int16" }, + { typeof(int), "std_msgs/Int32" }, + { typeof(long), "std_msgs/Int64" }, + { typeof(byte), "std_msgs/UInt8" }, + { typeof(ushort), "std_msgs/UInt16" }, + { typeof(uint), "std_msgs/UInt32" }, + { typeof(ulong), "std_msgs/UInt64" }, + { typeof(float), "std_msgs/Float32" }, + { typeof(double), "std_msgs/Float64" }, + { typeof(string), "std_msgs/String" }, }; - public struct PartialByteArray - { - public byte[] Array; - public int Length; - - public string Base64; - } - public static void Escape(StringBuilder sb, string text) { foreach (char c in text) @@ -571,10 +557,8 @@ static bool CheckBasicType(Type type) return false; } - public static void SerializeInternal(int version, StringBuilder sb, Type type, object message, SerialType sType = SerialType.JSON, string keyName = "") + public static void SerializeInternal(int version, StringBuilder sb, Type type, object message, string keyName = "") { - var nulChr = (object)null; - if (type.IsNullable()) { type = Nullable.GetUnderlyingType(type); @@ -596,15 +580,7 @@ public static void SerializeInternal(int version, StringBuilder sb, Type type, o } else if (type.IsEnum) { - if (sType == SerialType.JSON) - { - var etype = type.GetEnumUnderlyingType(); - SerializeInternal(version, sb, etype, Convert.ChangeType(message, etype), sType: sType); - } - else if (sType == SerialType.HDMap) - { - sb.Append(message.ToString()); - } + sb.Append(message.ToString()); } else if (BuiltinMessageTypes.ContainsKey(type)) { @@ -617,36 +593,6 @@ public static void SerializeInternal(int version, StringBuilder sb, Type type, o sb.Append(message.ToString()); } } - else if (type == typeof(PartialByteArray) && sType == SerialType.JSON) - { - PartialByteArray arr = (PartialByteArray)message; - if (version == 1) - { - sb.Append('"'); - if (arr.Base64 == null) - { - sb.Append(System.Convert.ToBase64String(arr.Array, 0, arr.Length)); - } - else - { - sb.Append(arr.Base64); - } - sb.Append('"'); - } - else - { - sb.Append(sType == SerialType.JSON ? '[' : nulChr); - for (int i = 0; i < arr.Length; i++) - { - sb.Append(arr.Array[i]); - if (i < arr.Length - 1) - { - sb.Append(sType == SerialType.JSON ? ',' : ' '); - } - } - sb.Append(sType == SerialType.JSON ? ']' : nulChr); - } - } else if (type.IsArray) { if (type.GetElementType() == typeof(byte) && version == 1) @@ -658,39 +604,35 @@ public static void SerializeInternal(int version, StringBuilder sb, Type type, o else { Array arr = (Array)message; - sb.Append(sType == SerialType.JSON ? '[' : nulChr); for (int i = 0; i < arr.Length; i++) { - if (sType == SerialType.HDMap && i > 0) + if (i > 0) { sb.Append(keyName); } - SerializeInternal(version, sb, type.GetElementType(), arr.GetValue(i), sType: sType); + SerializeInternal(version, sb, type.GetElementType(), arr.GetValue(i)); if (i < arr.Length - 1) { - sb.Append(sType == SerialType.JSON ? ',' : ' '); + sb.Append(' '); } } - sb.Append(sType == SerialType.JSON ? ']' : nulChr); } } else if (type.IsGenericList()) { IList list = (IList)message; - sb.Append(sType == SerialType.JSON ? '[' : nulChr); for (int i = 0; i < list.Count; i++) { - if (sType == SerialType.HDMap && i > 0) + if (i > 0) { sb.Append(keyName); } - SerializeInternal(version, sb, list[i].GetType(), list[i], sType: sType); + SerializeInternal(version, sb, list[i].GetType(), list[i]); if (i < list.Count - 1) { - sb.Append(sType == SerialType.JSON ? ',' : ' '); + sb.Append(' '); } } - sb.Append(sType == SerialType.JSON ? ']' : nulChr); } else if (type == typeof(Time)) { @@ -733,54 +675,27 @@ public static void SerializeInternal(int version, StringBuilder sb, Type type, o var oneFieldValue = oneInfo.Value; var oneFieldType = oneInfo.Value.GetType(); - sb.Append(sType == SerialType.JSON ? '"' : nulChr); sb.Append(oneFieldName); - sb.Append(sType == SerialType.JSON ? '"' : nulChr); - if (sType == SerialType.HDMap) - { - if (CheckBasicType(oneFieldType) || (oneFieldType.IsCollectionType() && CheckBasicType(oneFieldType.GetCollectionElement()))) - { - sb.Append(':'); - } - SerializeInternal(version, sb, oneFieldType, oneFieldValue, sType: sType, keyName: oneFieldName); - } - else if (sType == SerialType.JSON) + if (CheckBasicType(oneFieldType) || (oneFieldType.IsCollectionType() && CheckBasicType(oneFieldType.GetCollectionElement()))) { sb.Append(':'); - SerializeInternal(version, sb, oneFieldType, oneFieldValue, sType: sType); } - sb.Append(sType == SerialType.JSON ? ',' : ' '); + SerializeInternal(version, sb, oneFieldType, oneFieldValue, oneFieldName); + + sb.Append(' '); } } } else if (fieldValue != null || (fieldType.IsNullable() && Attribute.IsDefined(field, typeof(global::Apollo.RequiredAttribute)))) { - sb.Append(sType == SerialType.JSON ? '"' : nulChr); sb.Append(field.Name); - sb.Append(sType == SerialType.JSON ? '"' : nulChr); - - if (sType == SerialType.HDMap) - { - if (CheckBasicType(fieldType) || (fieldType.IsCollectionType() && CheckBasicType(fieldType.GetCollectionElement()))) - { - sb.Append(':'); - } - SerializeInternal(version, sb, fieldType, fieldValue, sType: sType, keyName: field.Name); - } - else if (sType == SerialType.JSON) + if (CheckBasicType(fieldType) || (fieldType.IsCollectionType() && CheckBasicType(fieldType.GetCollectionElement()))) { sb.Append(':'); - SerializeInternal(version, sb, fieldType, fieldValue, sType: sType); } - sb.Append(sType == SerialType.JSON ? ',' : ' '); - } - } - if (sType == SerialType.JSON) - { - if (sb[sb.Length - 1] == ',') - { - sb.Remove(sb.Length - 1, 1); + SerializeInternal(version, sb, fieldType, fieldValue, field.Name); + sb.Append(' '); } } diff --git a/Assets/Scripts/Ros/RosBridge.cs b/Assets/Scripts/Ros/RosBridge.cs index 28b81544..0b330100 100644 --- a/Assets/Scripts/Ros/RosBridge.cs +++ b/Assets/Scripts/Ros/RosBridge.cs @@ -421,10 +421,8 @@ static void Escape(StringBuilder sb, string text) } } - public static void SerializeInternal(int version, StringBuilder sb, Type type, object message, SerialType sType = SerialType.JSON, string keyName = "") + public static void SerializeInternal(int version, StringBuilder sb, Type type, object message, string keyName = "") { - var nulChr = (object)null; - if (type.IsNullable()) { type = Nullable.GetUnderlyingType(type); @@ -445,15 +443,8 @@ public static void SerializeInternal(int version, StringBuilder sb, Type type, o } else if (type.IsEnum) { - if (sType == SerialType.JSON) - { - var etype = type.GetEnumUnderlyingType(); - SerializeInternal(version, sb, etype, Convert.ChangeType(message, etype), sType: sType); - } - else if (sType == SerialType.HDMap) - { - sb.Append(message.ToString()); - } + var etype = type.GetEnumUnderlyingType(); + SerializeInternal(version, sb, etype, Convert.ChangeType(message, etype)); } else if (BuiltinMessageTypes.ContainsKey(type)) { @@ -466,9 +457,9 @@ public static void SerializeInternal(int version, StringBuilder sb, Type type, o sb.Append(message.ToString()); } } - else if (type == typeof(global::Ros.PartialByteArray) && sType == SerialType.JSON) + else if (type == typeof(global::Ros.PartialByteArray)) { - global::Ros.PartialByteArray arr = (global::Ros.PartialByteArray)message; + var arr = (global::Ros.PartialByteArray)message; if (version == 1) { sb.Append('"'); @@ -484,16 +475,16 @@ public static void SerializeInternal(int version, StringBuilder sb, Type type, o } else { - sb.Append(sType == SerialType.JSON ? '[' : nulChr); + sb.Append('['); for (int i = 0; i < arr.Length; i++) { sb.Append(arr.Array[i]); if (i < arr.Length - 1) { - sb.Append(sType == SerialType.JSON ? ',' : ' '); + sb.Append(','); } } - sb.Append(sType == SerialType.JSON ? ']' : nulChr); + sb.Append(']'); } } else if (type.IsArray) @@ -507,39 +498,31 @@ public static void SerializeInternal(int version, StringBuilder sb, Type type, o else { Array arr = (Array)message; - sb.Append(sType == SerialType.JSON ? '[' : nulChr); + sb.Append('['); for (int i = 0; i < arr.Length; i++) { - if (sType == SerialType.HDMap && i > 0) - { - sb.Append(keyName); - } - SerializeInternal(version, sb, type.GetElementType(), arr.GetValue(i), sType: sType); + SerializeInternal(version, sb, type.GetElementType(), arr.GetValue(i)); if (i < arr.Length - 1) { - sb.Append(sType == SerialType.JSON ? ',' : ' '); + sb.Append(','); } } - sb.Append(sType == SerialType.JSON ? ']' : nulChr); + sb.Append(']'); } } else if (type.IsGenericList()) { IList list = (IList)message; - sb.Append(sType == SerialType.JSON ? '[' : nulChr); + sb.Append('['); for (int i = 0; i < list.Count; i++) { - if (sType == SerialType.HDMap && i > 0) - { - sb.Append(keyName); - } - SerializeInternal(version, sb, list[i].GetType(), list[i], sType: sType); + SerializeInternal(version, sb, list[i].GetType(), list[i]); if (i < list.Count - 1) { - sb.Append(sType == SerialType.JSON ? ',' : ' '); + sb.Append(','); } } - sb.Append(sType == SerialType.JSON ? ']' : nulChr); + sb.Append(']'); } else if (type == typeof(global::Ros.Time)) { @@ -582,55 +565,29 @@ public static void SerializeInternal(int version, StringBuilder sb, Type type, o var oneFieldValue = oneInfo.Value; var oneFieldType = oneInfo.Value.GetType(); - sb.Append(sType == SerialType.JSON ? '"' : nulChr); + sb.Append('"'); sb.Append(oneFieldName); - sb.Append(sType == SerialType.JSON ? '"' : nulChr); - - if (sType == SerialType.HDMap) - { - if (CheckBasicType(oneFieldType) || (oneFieldType.IsCollectionType() && CheckBasicType(oneFieldType.GetCollectionElement()))) - { - sb.Append(':'); - } - SerializeInternal(version, sb, oneFieldType, oneFieldValue, sType: sType, keyName: oneFieldName); - } - else if (sType == SerialType.JSON) - { - sb.Append(':'); - SerializeInternal(version, sb, oneFieldType, oneFieldValue, sType: sType); - } - sb.Append(sType == SerialType.JSON ? ',' : ' '); + sb.Append('"'); + sb.Append(':'); + SerializeInternal(version, sb, oneFieldType, oneFieldValue); + sb.Append(','); } } } else if (fieldValue != null || (fieldType.IsNullable() && Attribute.IsDefined(field, typeof(global::Apollo.RequiredAttribute)))) { - sb.Append(sType == SerialType.JSON ? '"' : nulChr); + sb.Append('"'); sb.Append(field.Name); - sb.Append(sType == SerialType.JSON ? '"' : nulChr); - - if (sType == SerialType.HDMap) - { - if (CheckBasicType(fieldType) || (fieldType.IsCollectionType() && CheckBasicType(fieldType.GetCollectionElement()))) - { - sb.Append(':'); - } - SerializeInternal(version, sb, fieldType, fieldValue, sType: sType, keyName: field.Name); - } - else if (sType == SerialType.JSON) - { - sb.Append(':'); - SerializeInternal(version, sb, fieldType, fieldValue, sType: sType); - } - sb.Append(sType == SerialType.JSON ? ',' : ' '); + sb.Append('"'); + sb.Append(':'); + SerializeInternal(version, sb, fieldType, fieldValue); + sb.Append(','); } } - if (sType == SerialType.JSON) + + if (sb[sb.Length - 1] == ',') { - if (sb[sb.Length - 1] == ',') - { - sb.Remove(sb.Length - 1, 1); - } + sb.Remove(sb.Length - 1, 1); } sb.Append('}'); @@ -664,7 +621,7 @@ static public void Serialize(int version, StringBuilder sb, Type type, object me } else { - SerializeInternal(version, sb, type, message, sType: SerialType.JSON); + SerializeInternal(version, sb, type, message); } } diff --git a/Assets/Scripts/RosWriter.cs b/Assets/Scripts/RosWriter.cs index 1eac8e4c..93a203d8 100644 --- a/Assets/Scripts/RosWriter.cs +++ b/Assets/Scripts/RosWriter.cs @@ -6,12 +6,6 @@ namespace Comm { namespace Ros { - public enum SerialType - { - JSON, - HDMap, - } - public class RosWriter : Writer { Bridge Bridge;