public Dictionary <K, V> toMap <K, V>(EzyObject obj) { Dictionary <K, V> answer = new Dictionary <K, V>(); foreach (Object key in obj.keys()) { Object value = obj.get <Object>(key); Object skey = key; EzyArrayToList arrayToList = EzyArrayToList.getInstance(); if (key is EzyArray) { skey = arrayToList.toList <object>((EzyArray)key); } else if (key is EzyObject) { skey = toMap <object, object>((EzyObject)key); } Object svalue = value; if (value != null) { if (value is EzyArray) { svalue = arrayToList.toList <object>((EzyArray)value); } if (value is EzyObject) { svalue = toMap <object, object>((EzyObject)value); } } answer[(K)skey] = (V)svalue; } return(answer); }
protected override EzyObject objectToMap(T obj, EzyMarshaller marshaller) { EzyObject map = EzyEntityFactory.newObject(); foreach (PropertyInfo property in objectType.GetProperties()) { string key = null; EzyValue anno = property.GetCustomAttribute <EzyValue>(); if (anno != null) { key = anno.name; } else { key = property.Name.Length <= 1 ? char.ToLower(property.Name[0]).ToString() : char.ToLower(property.Name[0]) + property.Name.Substring(1); } object rawValue = property.GetValue(obj); object value = rawValue != null?marshaller.marshall <object>(rawValue) : null; map.put(key, value); } return(map); }
public IDictionary <K, V> unmarshallDict <K, V>(EzyObject obj) { IDictionary <K, V> answer = new Dictionary <K, V>(); foreach (object key in obj.keys()) { answer[unmarshall <K>(key)] = unmarshall <V>(obj.get <V>(key)); } return(answer); }
public void handle(EzyApp app, EzyData d) { EzyObject data = (EzyObject)d; long currentRoomId = data.get <long>("currentRoomId"); if (currentRoomId <= 0) { app.send(Commands.JOIN_OR_CREATE_ROOM); } else { app.send(Commands.RECONNECT); } }
protected byte[] parseObject(EzyObject obj) { int index = 1; int size = obj.size(); byte[][] bytess = new byte[size * 2 + 1][]; bytess[0] = parseMapSize(size); foreach (Object key in obj.keys()) { bytess[index++] = serialize(key); bytess[index++] = serialize(obj.get <Object>(key)); } return(EzyBytes.merge(bytess)); }
public object unmarshallByOutType(object input, Type outType) { if (input == null) { return(null); } if (readerByOutType.ContainsKey(outType)) { IEzyReader reader = readerByOutType[outType]; return(reader.read(input, this)); } if (outType.IsGenericType) { if (typeof(IDictionary).IsAssignableFrom(outType) || typeof(IDictionary <,>) == outType.GetGenericTypeDefinition()) { Type dictType = typeof(Dictionary <,>); Type constructed = dictType.MakeGenericType(outType.GetGenericArguments()); IDictionary answer = (IDictionary)Activator.CreateInstance(constructed); EzyObject obj = (EzyObject)input; Type keyType = outType.GetGenericArguments()[0]; Type valueType = outType.GetGenericArguments()[1]; foreach (object key in obj.keys()) { answer[unmarshallByOutType(key, keyType)] = unmarshallByOutType(obj.getByOutType(key, valueType), valueType); } return(answer); } else if (typeof(IList).IsAssignableFrom(outType) || typeof(IList <>) == outType.GetGenericTypeDefinition()) { Type listType = typeof(List <>); Type constructed = listType.MakeGenericType(outType.GetGenericArguments()); IList answer = (IList)Activator.CreateInstance(constructed); EzyArray array = (EzyArray)input; Type valueType = outType.GetGenericArguments()[0]; for (int i = 0; i < array.size(); ++i) { Object rawValue = array.getByOutType(i, valueType); Object value = unmarshallByOutType(rawValue, valueType); answer.Add(value); } return(answer); } } return(input); }
public object read(object input, EzyUnmarshaller unmarshaller) { EzyObject map = null; if (input is IDictionary) { map = EzyEntityFactory.newObjectBuilder() .appendRawDict((IDictionary)input) .build(); } else { map = (EzyObject)input; } return(mapToObject(map, unmarshaller)); }
public object marshallByInType(object input, Type inType) { if (input == null) { return(null); } if (writerByInType.ContainsKey(inType)) { IEzyWriter writer = writerByInType[inType]; return(writer.write(input, this)); } if (typeof(IDictionary).IsAssignableFrom(inType)) { EzyObject answer = EzyEntityFactory.newObject(); IDictionary dict = (IDictionary)(input); Type keyType = inType.GetGenericArguments()[0]; Type valueType = inType.GetGenericArguments()[1]; foreach (DictionaryEntry entry in dict) { answer.put( marshallByInType(entry.Key, keyType), marshallByInType(entry.Value, valueType)); } return(answer); } else if (typeof(IList).IsAssignableFrom(inType)) { EzyArray answer = EzyEntityFactory.newArray(); IList list = (IList)(input); Type valueType = inType.GetGenericArguments()[0]; foreach (Object value in list) { answer.add(marshallByInType(value, valueType)); } return(answer); } return(input); }
protected Object transformNonNullValue(Object value) { if (value is IDictionary) { IDictionary dictionary = (IDictionary)value; EzyObject obj = EzyEntityFactory.newObject(); foreach (DictionaryEntry entry in dictionary) { obj.put(transform(entry.Key), transform(entry.Value)); } return(obj); } if (value is ICollection) { IEnumerable collection = (IEnumerable)value; EzyArray array = EzyEntityFactory.newArray(); foreach (Object item in collection) { array.add(transform(item)); } return(array); } return(value); }
public EzyObject newObject() { EzyObject obj = new EzyObject(INPUT_TRANSFORMER, OUTPUT_TRANSFORMER); return(obj); }
public static EzyObject newObject <K, V>(IDictionary <K, V> dict) { EzyObject obj = newObjectBuilder().append(dict).build(); return(obj); }
public void emitStartGame(EzyObject data) { startGameCallback(data); }
public void emitGameIdReceived(EzyObject data) { gameIdReceivedCallback(data); }
protected override T mapToObject(EzyObject map, EzyUnmarshaller unmarshaller) { PropertyInfo[] properties = objectType.GetProperties(); T obj = (T)Activator.CreateInstance(objectType); foreach (PropertyInfo property in properties) { Type outType = property.PropertyType; object rawValue = null; EzyValue anno = property.GetCustomAttribute <EzyValue>(); if (anno != null) { rawValue = map.getByOutType(anno.name, outType); } else { rawValue = map.getByOutType(property.Name, outType); if (rawValue == null) { string keyString = char.ToLower(property.Name[0]).ToString(); if (property.Name.Length > 1) { keyString += property.Name.Substring(1); } rawValue = map.getByOutType(keyString, outType); } } if (rawValue == null) { continue; } object value = unmarshaller.unmarshallByOutType(rawValue, outType); if (outType == value.GetType()) { property.SetValue(obj, value); } else { MethodInfo parseMethod = property.PropertyType.GetMethod( "TryParse", BindingFlags.Public | BindingFlags.Static, null, new[] { typeof(string), property.PropertyType.MakeByRefType() }, null ); if (parseMethod != null) { object[] parameters = new[] { value, null }; bool success = (bool)parseMethod.Invoke(null, parameters); if (success) { property.SetValue(obj, parameters[1]); } } } } return(obj); }
protected abstract T mapToObject(EzyObject map, EzyUnmarshaller unmarshaller);
public void emitReconnected(EzyObject data) { appAccessed = true; reconnectedCallback(data); }
public void Run() { IDictionary <String, Double> dict1 = new Dictionary <String, Double>(); dict1["a"] = 1.0D; dict1["b"] = 2.0F; dict1["sbyte"] = (sbyte)100; dict1["byte"] = (byte)101; dict1["double"] = (double)102.3D; dict1["float"] = (float)103.4F; dict1["int"] = (int)104; dict1["long"] = (long)105; dict1["short"] = (short)106; IList <Object> list1 = new List <Object>(); list1.Add(dict1); list1.Add((Int64)100); IDictionary <Object, Object> dict2 = new Dictionary <Object, Object>(); dict2[list1] = dict1; dict2["Hello"] = "World"; dict2["c"] = 3.0D; dict2[dict1] = list1; List <Object> list2 = new List <object>(); list2.Add(dict1); list2.Add(dict2); list2.Add(list1); EzyArray array = EzyEntityFactory.newArrayBuilder() .append(dict1) .append(dict2) .append(list1) .appendAll(list2) .build(); EzyObject obj = EzyEntityFactory.newObjectBuilder() .append(dict1) .append(dict2) .append("list1", list1) .append("list2", list2) .build(); int expectedListCount = 1 + 1 + 1 + list2.Count; if (array.size() != expectedListCount) { throw new ArgumentException("expected: " + expectedListCount + " but: " + array.size()); } int expectedDictCount = dict1.Count + dict2.Count + 1 + 1; if (obj.size() != expectedDictCount) { throw new ArgumentException("expected: " + expectedDictCount + " but: " + array.size()); } Console.WriteLine("array: " + array); Console.WriteLine("\n\nobj: " + obj); Console.WriteLine("\n\nclone array: " + array.Clone()); Console.WriteLine("\n\nclone obj: " + obj.Clone()); Console.WriteLine("\n\nlong from double" + obj.get <long>("a")); Console.WriteLine("sbyte from sbyte: " + obj.get <sbyte>("sbyte")); Console.WriteLine("byte from sbyte: " + obj.get <byte>("sbyte")); Console.WriteLine("double from sbyte: " + obj.get <double>("sbyte")); Console.WriteLine("float from sbyte: " + obj.get <float>("sbyte")); Console.WriteLine("int from sbyte: " + obj.get <int>("sbyte")); Console.WriteLine("long from sbyte: " + obj.get <long>("sbyte")); Console.WriteLine("short from sbyte: " + obj.get <short>("sbyte")); Console.WriteLine("sbyte from byte: " + obj.get <sbyte>("byte")); Console.WriteLine("byte from byte: " + obj.get <byte>("byte")); Console.WriteLine("double from byte: " + obj.get <double>("byte")); Console.WriteLine("float from byte: " + obj.get <float>("byte")); Console.WriteLine("int from byte: " + obj.get <int>("byte")); Console.WriteLine("long from byte: " + obj.get <long>("byte")); Console.WriteLine("short from byte: " + obj.get <short>("byte")); Console.WriteLine("sbyte from double: " + obj.get <double>("double")); Console.WriteLine("byte from double: " + obj.get <byte>("double")); Console.WriteLine("double from double: " + obj.get <double>("double")); Console.WriteLine("float from double: " + obj.get <float>("double")); Console.WriteLine("int from double: " + obj.get <int>("double")); Console.WriteLine("long from double: " + obj.get <long>("double")); Console.WriteLine("short from double: " + obj.get <short>("double")); Console.WriteLine("sbyte from float: " + obj.get <float>("float")); Console.WriteLine("byte from float: " + obj.get <byte>("float")); Console.WriteLine("double from float: " + obj.get <double>("float")); Console.WriteLine("float from float: " + obj.get <float>("float")); Console.WriteLine("int from float: " + obj.get <int>("float")); Console.WriteLine("long from float: " + obj.get <long>("float")); Console.WriteLine("short from float: " + obj.get <short>("float")); Console.WriteLine("sbyte from int: " + obj.get <int>("int")); Console.WriteLine("byte from int: " + obj.get <byte>("int")); Console.WriteLine("double from int: " + obj.get <double>("int")); Console.WriteLine("float from int: " + obj.get <float>("int")); Console.WriteLine("int from int: " + obj.get <int>("int")); Console.WriteLine("long from int: " + obj.get <long>("int")); Console.WriteLine("short from int: " + obj.get <short>("int")); Console.WriteLine("sbyte from long: " + obj.get <long>("long")); Console.WriteLine("byte from long: " + obj.get <byte>("long")); Console.WriteLine("double from long: " + obj.get <double>("long")); Console.WriteLine("float from long: " + obj.get <float>("long")); Console.WriteLine("int from long: " + obj.get <int>("long")); Console.WriteLine("long from long: " + obj.get <long>("long")); Console.WriteLine("short from long: " + obj.get <short>("long")); Console.WriteLine("sbyte from short: " + obj.get <short>("short")); Console.WriteLine("byte from short: " + obj.get <byte>("short")); Console.WriteLine("double from short: " + obj.get <double>("short")); Console.WriteLine("float from short: " + obj.get <float>("short")); Console.WriteLine("int from short: " + obj.get <int>("short")); Console.WriteLine("long from short: " + obj.get <long>("short")); Console.WriteLine("short from short: " + obj.get <short>("short")); List <object> l = array.toList <object>(); IDictionary <object, object> d = obj.toDict <object, object>(); if (l.Count != expectedListCount) { throw new ArgumentException("expected: " + expectedListCount + " but: " + l.Count); } if (d.Count != expectedDictCount) { throw new ArgumentException("expected: " + expectedDictCount + " but: " + d.Count); } Console.WriteLine("list: " + l); Console.WriteLine("dict: " + d); }