-
Notifications
You must be signed in to change notification settings - Fork 5
/
RosSerialization.cs
364 lines (342 loc) · 11.4 KB
/
RosSerialization.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/**
* Copyright (c) 2020 LG Electronics, Inc.
*
* This software contains code licensed as described in LICENSE.
*
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using System.Text;
using SimpleJSON;
namespace Simulator.Bridge.Ros
{
// fill either Array & Length, or set Base64 string
public class PartialByteArray
{
public byte[] Array;
public int Length;
public string Base64;
}
static class RosSerialization
{
public static readonly Dictionary<Type, string> BuiltinMessageTypes = new Dictionary<Type, 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" },
};
static bool CheckBasicType(Type type)
{
if (type.IsNullable())
{
type = Nullable.GetUnderlyingType(type);
}
if (BuiltinMessageTypes.ContainsKey(type))
{
return true;
}
if (type == typeof(string))
{
return true;
}
if (type.IsEnum)
{
return true;
}
return false;
}
static void SerializeInternal(object message, Type type, StringBuilder sb)
{
if (type.IsNullable())
{
type = Nullable.GetUnderlyingType(type);
}
if (type == typeof(string))
{
var str = message as string;
sb.Append('"');
if (!string.IsNullOrEmpty(str))
{
sb.AppendEscaped(str);
}
sb.Append('"');
}
else if (type.IsEnum)
{
var etype = type.GetEnumUnderlyingType();
SerializeInternal(Convert.ChangeType(message, etype), etype, sb);
}
else if (BuiltinMessageTypes.ContainsKey(type))
{
if (type == typeof(bool))
{
sb.Append(string.Format(CultureInfo.InvariantCulture, "{0}", message).ToLowerInvariant());
}
else
{
sb.Append(string.Format(CultureInfo.InvariantCulture, "{0}", message));
}
}
else if (type == typeof(PartialByteArray))
{
var arr = message as PartialByteArray;
sb.Append('"');
if (arr.Base64 == null)
{
sb.Append(Convert.ToBase64String(arr.Array, 0, arr.Length));
}
else
{
sb.Append(arr.Base64);
}
sb.Append('"');
}
else if (type.IsArray)
{
if (type.GetElementType() == typeof(byte))
{
sb.Append('"');
sb.Append(Convert.ToBase64String((byte[])message));
sb.Append('"');
}
else
{
var array = message as Array;
var elementType = type.GetElementType();
sb.Append('[');
for (int i = 0; i < array.Length; i++)
{
SerializeInternal(array.GetValue(i), elementType, sb);
if (i < array.Length - 1)
{
sb.Append(',');
}
}
sb.Append(']');
}
}
else if (type.IsGenericList())
{
var list = message as IList;
var elementType = type.GetGenericArguments()[0];
sb.Append('[');
for (int i = 0; i < list.Count; i++)
{
SerializeInternal(list[i], elementType, sb);
if (i < list.Count - 1)
{
sb.Append(',');
}
}
sb.Append(']');
}
else if (type == typeof(Ros.Time))
{
var t = message as Ros.Time;
sb.AppendFormat("{{\"secs\":{0},\"nsecs\":{1}}}", (uint)t.secs, (uint)t.nsecs);
}
else
{
var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
sb.Append('{');
for (int i = 0; i < fields.Length; i++)
{
var field = fields[i];
var fieldType = field.FieldType;
var fieldValue = field.GetValue(message);
if (fieldValue != null)
{
sb.Append('"');
sb.Append(field.Name);
sb.Append('"');
sb.Append(':');
SerializeInternal(fieldValue, fieldType, sb);
sb.Append(',');
}
}
if (sb[sb.Length - 1] == ',')
{
sb.Remove(sb.Length - 1, 1);
}
sb.Append('}');
}
}
public static void Serialize(object message, StringBuilder sb)
{
var type = message.GetType();
if (type == typeof(string))
{
var str = message as string;
sb.Append("{");
sb.Append("\"data\":");
sb.Append('"');
sb.AppendEscaped(str);
sb.Append('"');
sb.Append('}');
}
else if (BuiltinMessageTypes.ContainsKey(type))
{
sb.Append("{");
sb.Append("\"data\":");
sb.Append(string.Format(CultureInfo.InvariantCulture, "{0}", message));
sb.Append('}');
}
else if (type == typeof(Ros.Time))
{
sb.Append("{");
sb.Append("\"data\":");
SerializeInternal(message, type, sb);
sb.Append('}');
}
else
{
SerializeInternal(message, type, sb);
}
}
static object UnserializeInternal(JSONNode node, Type type)
{
if (type == typeof(bool))
{
return node.AsBool;
}
else if (type == typeof(sbyte))
{
return short.Parse(node.Value);
}
else if (type == typeof(int))
{
return int.Parse(node.Value);
}
else if (type == typeof(long))
{
return long.Parse(node.Value);
}
else if (type == typeof(byte))
{
return byte.Parse(node.Value);
}
else if (type == typeof(ushort))
{
return ushort.Parse(node.Value);
}
else if (type == typeof(uint))
{
return uint.Parse(node.Value);
}
else if (type == typeof(ulong))
{
return ulong.Parse(node.Value);
}
else if (type == typeof(float))
{
return node.AsFloat;
}
else if (type == typeof(double))
{
return node.AsDouble;
}
else if (type == typeof(string))
{
return node.Value;
}
else if (type == typeof(PartialByteArray))
{
var nodeArr = node.AsArray;
if (type.GetElementType() == typeof(byte) && nodeArr == null)
{
var array = Convert.FromBase64String(node.Value);
return new PartialByteArray()
{
Array = array,
Length = array.Length,
};
}
else
{
var array = new PartialByteArray()
{
Array = new byte[node.Count],
Length = node.Count,
};
for (int i = 0; i < node.Count; i++)
{
array.Array[i] = byte.Parse(nodeArr[i].Value);
}
return array;
}
}
else if (type.IsArray)
{
var nodeArr = node.AsArray;
if (type.GetElementType() == typeof(byte) && nodeArr == null)
{
return Convert.FromBase64String(node.Value);
}
var arr = Array.CreateInstance(type.GetElementType(), node.Count);
for (int i = 0; i < node.Count; i++)
{
arr.SetValue(UnserializeInternal(nodeArr[i], type.GetElementType()), i);
}
return arr;
}
else if (type == typeof(Ros.Time))
{
var nodeObj = node.AsObject;
return new Ros.Time()
{
secs = uint.Parse(nodeObj["secs"].Value),
nsecs = uint.Parse(nodeObj["nsecs"].Value),
};
}
else if (type.IsEnum)
{
var value = node.AsInt;
var obj = Enum.ToObject(type, value);
return obj;
}
else
{
var nodeObj = node.AsObject;
var obj = Activator.CreateInstance(type);
foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.Instance))
{
if (nodeObj[field.Name] != null)
{
var fieldType = field.FieldType;
if (fieldType.IsNullable())
{
fieldType = Nullable.GetUnderlyingType(fieldType);
}
var value = UnserializeInternal(nodeObj[field.Name], fieldType);
field.SetValue(obj, value);
}
}
return obj;
}
}
public static object Unserialize(JSONNode node, Type type)
{
if (BuiltinMessageTypes.ContainsKey(type) || type == typeof(Ros.Time))
{
return UnserializeInternal(node["data"], type);
}
return UnserializeInternal(node, type);
}
public static BridgeType Unserialize<BridgeType>(JSONNode node)
{
return (BridgeType)Unserialize(node, typeof(BridgeType));
}
}
}