public override OnlineMapsJSONItem GetAll(string k) { OnlineMapsJSONItem item = GetThis(k); OnlineMapsJSONArray arr = null; if (item != null) { arr = new OnlineMapsJSONArray(); arr.Add(item); } var enumerator = _table.GetEnumerator(); while (enumerator.MoveNext()) { item = enumerator.Current.Value; OnlineMapsJSONArray subArr = item.GetAll(k) as OnlineMapsJSONArray; if (subArr != null) { if (arr == null) { arr = new OnlineMapsJSONArray(); } arr.AddRange(subArr); } } return(arr); }
/// <summary> /// Using selectors /// </summary> /// <param name="json">JSON object</param> private void UsingSelectors(OnlineMapsJSONItem json) { // Get the text element that is the child of the current node. OnlineMapsJSONItem text = json["text"]; Debug.Log(text.V <string>()); // Gets the second element in the items array. OnlineMapsJSONItem item = json["items/1"]; Debug.Log(item.ToString()); // Gets the second element, the first element of the items array. In this case - x. OnlineMapsJSONItem x = json["items/0/x"]; Debug.Log(x.V <int>()); // Gets all the name elements in the items array. OnlineMapsJSONItem names = json["items/*/name"]; Debug.Log(names.ToString()); // Looks for id elements, in the child element of any nesting. OnlineMapsJSONItem ids = json["//id"]; Debug.Log(ids.ToString()); }
public override OnlineMapsJSONItem GetAll(string k) { OnlineMapsJSONItem item = GetThis(k); OnlineMapsJSONArray arr = null; if (item != null) { arr = new OnlineMapsJSONArray(); arr.Add(item); } for (int i = 0; i < _count; i++) { item = array[i]; OnlineMapsJSONArray subArr = item.GetAll(k) as OnlineMapsJSONArray; if (subArr != null) { if (arr == null) { arr = new OnlineMapsJSONArray(); } arr.AddRange(subArr); } } return(arr); }
public void LoadSettings(OnlineMapsJSONItem json) { OnlineMapsJSONItem jitems = json["items"]; RemoveAll(); foreach (OnlineMapsJSONItem jitem in jitems) { OnlineMapsMarker3D marker = new OnlineMapsMarker3D(); double mx = jitem.ChildValue <double>("longitude"); double my = jitem.ChildValue <double>("latitude"); marker.SetPosition(mx, my); marker.range = jitem.ChildValue <OnlineMapsRange>("range"); marker.label = jitem.ChildValue <string>("label"); marker.prefab = OnlineMapsUtils.GetObject(jitem.ChildValue <int>("prefab")) as GameObject; marker.rotationY = jitem.ChildValue <float>("rotationY"); marker.scale = jitem.ChildValue <float>("scale"); marker.enabled = jitem.ChildValue <bool>("enabled"); Add(marker); } (json["settings"] as OnlineMapsJSONObject).DeserializeObject(this); }
/// <summary> /// Load items and component settings from JSON /// </summary> /// <param name="json">JSON item</param> public void LoadSettings(OnlineMapsJSONItem json) { OnlineMapsJSONItem jitems = json["items"]; RemoveAll(); foreach (OnlineMapsJSONItem jitem in jitems) { OnlineMapsMarker marker = new OnlineMapsMarker(); double mx = jitem.ChildValue <double>("longitude"); double my = jitem.ChildValue <double>("latitude"); marker.SetPosition(mx, my); marker.range = jitem.ChildValue <OnlineMapsRange>("range"); marker.label = jitem.ChildValue <string>("label"); marker.texture = OnlineMapsUtils.GetObject(jitem.ChildValue <int>("texture")) as Texture2D; marker.align = (OnlineMapsAlign)jitem.ChildValue <int>("align"); marker.rotation = jitem.ChildValue <float>("rotation"); marker.enabled = jitem.ChildValue <bool>("enabled"); Add(marker); } OnlineMapsJSONItem jsettings = json["settings"]; defaultTexture = OnlineMapsUtils.GetObject(jsettings.ChildValue <int>("defaultTexture")) as Texture2D; defaultAlign = (OnlineMapsAlign)jsettings.ChildValue <int>("defaultAlign"); defaultScale = jsettings.ChildValue <float>("defaultScale"); allowAddMarkerByM = jsettings.ChildValue <bool>("allowAddMarkerByM"); }
/// <summary> /// Full deserialization of JSON object into an object /// </summary> /// <param name="json">JSON object</param> private void FullDeserialization(OnlineMapsJSONItem json) { // Full deserialization Data data = json.Deserialize <Data>(); Debug.Log(data.text); // Deserialization into object with alias Data2 data2 = json.Deserialize <Data2>(); Debug.Log(data2.lItems[0].name); }
protected override OnlineMapsJSONItem SaveSettings() { OnlineMapsJSONItem jitem = base.SaveSettings(); jitem["settings"].AppendObject(new { allowAddMarker3DByN, defaultPrefab = defaultPrefab != null? defaultPrefab.GetInstanceID(): -1, defaultScale }); return(jitem); }
protected override OnlineMapsJSONItem SaveSettings() { OnlineMapsJSONItem jitem = base.SaveSettings(); jitem["settings"].AppendObject(new { defaultTexture = defaultTexture != null? defaultTexture.GetInstanceID(): -1, defaultAlign = (int)defaultAlign, allowAddMarkerByM }); return(jitem); }
private void OnReverseGeocodeComplete(OnlineMapsWWW www) { if (www.hasError) { Debug.Log(www.error); return; } OnlineMapsJSONItem json = OnlineMapsJSON.Parse(www.text); Debug.Log(json["address/LongLabel"].V <string>()); }
protected override OnlineMapsJSONItem SaveSettings() { OnlineMapsJSONItem json = base.SaveSettings(); json.AppendObject(new { marker2DMode, marker2DSize, activeCamera }); return(json); }
/// <summary> /// How to go through all the children /// </summary> /// <param name="json">JSON object</param> private void UsingForEach(OnlineMapsJSONItem json) { // Go through all the elements in the items. foreach (OnlineMapsJSONItem item in json["items"]) { Debug.Log(item.ChildValue <int>("x")); } // Very often you need to know the key of the element, you can do it this way. foreach (KeyValuePair <string, OnlineMapsJSONItem> pair in (json as OnlineMapsJSONObject).table) { Debug.Log(pair.Key); } }
private OnlineMapsJSONItem SaveSettings() { OnlineMapsJSONItem json = OnlineMapsJSON.Serialize(new { zoomRange, levelsRange, levelHeight, minHeight, heightScale, maxBuilding, maxActiveBuildings, generateColliders, useColorTag, materials }); return(json); }
/// <summary> /// Combines two JSON Object. /// </summary> /// <param name="other">Other JSON Object</param> /// <param name="overwriteExistingValues">Overwrite the existing values?</param> public void Combine(OnlineMapsJSONItem other, bool overwriteExistingValues = false) { OnlineMapsJSONObject otherObj = other as OnlineMapsJSONObject; if (otherObj == null) { throw new Exception("Only OnlineMapsJSONObject is allowed to be combined."); } Dictionary <string, OnlineMapsJSONItem> otherDict = otherObj.table; foreach (KeyValuePair <string, OnlineMapsJSONItem> pair in otherDict) { if (overwriteExistingValues || !_table.ContainsKey(pair.Key)) { _table[pair.Key] = pair.Value; } } }
private OnlineMapsJSONItem GetThis(string key) { int kindex; if (key.Contains("/")) { int index = key.IndexOf("/"); string k = key.Substring(0, index); string nextPart = key.Substring(index + 1); if (k == "*") { OnlineMapsJSONArray arr = new OnlineMapsJSONArray(); for (int i = 0; i < _count; i++) { OnlineMapsJSONItem item = array[i][nextPart]; if (item != null) { arr.Add(item); } } return(arr); } if (int.TryParse(k, out kindex)) { if (kindex < 0 || kindex >= _count) { return(null); } OnlineMapsJSONItem item = array[kindex]; return(item[nextPart]); } } if (key == "*") { return(this); } if (int.TryParse(key, out kindex)) { return(this[kindex]); } return(null); }
/// <summary> /// Partial deserialization of JSON object /// </summary> /// <param name="json">JSON object</param> private void PartialDeserialization(OnlineMapsJSONItem json) { /* * Using OnlineMapsJSON you can select and deserialize only part of json. * This is very useful, especially if you are working with files from * third-party sources and you do not need all the data. */ // Select and deserialize the items list. // It does not matter that in the original data it was an array. List <Item> items = json["items"].Deserialize <List <Item> >(); Debug.Log(items.Count); // Select and deserialize all subItems OnlineMapsJSONItem item = json["//subItems"]; Item2[] subItems = item.Deserialize <Item2[]>(); Debug.Log(subItems.Length); }
/// <summary> /// How to get the value of JSON elements. /// </summary> /// <param name="json">JSON object</param> private void GetValues(OnlineMapsJSONItem json) { // Get the value of the text element. string text = json["text"].Value <string>(); // or in such a ways text = json.ChildValue <string>("text"); text = json.V <string>("text"); text = json["text"].V <string>(); Debug.Log(text); // You can get: string, bool, float, double, int, long, byte, short. int x = json["listItems/0/x"].Value <int>(); Debug.Log(x); // A value of any type can be read as a string. // In this case, y is int. string y = json["listItems/0/y"].Value <string>(); Debug.Log(y); }
private void OnGeocodeComplete(OnlineMapsWWW www) { if (www.hasError) { Debug.Log(www.error); return; } OnlineMapsJSONItem json = OnlineMapsJSON.Parse(www.text); OnlineMapsJSONItem firstItem = json["candidates/0"]; if (firstItem == null) { return; } OnlineMapsVector2d center = firstItem["location"].Deserialize <OnlineMapsVector2d>(); OnlineMapsJSONItem extent = firstItem["extent"]; double xmin = extent.V <double>("xmin"), ymin = extent.V <double>("ymin"), xmax = extent.V <double>("xmax"), ymax = extent.V <double>("ymax"); Vector2[] points = { new Vector2((float)xmin, (float)ymin), new Vector2((float)xmax, (float)ymax), }; Vector2 c; int zoom; OnlineMapsUtils.GetCenterPointAndZoom(points, out c, out zoom); OnlineMaps.instance.SetPositionAndZoom(center.x, center.y, zoom); }
public void Start() { // Generate random data object Data data = Data.Generate(); // Serialization an object OnlineMapsJSONItem json = Serialize(data); // Convert JSON object to a string string jsonString = json.ToString(); Debug.Log(jsonString); // Parsing JSON string json = Parse(jsonString); // Full deserialization of JSON object FullDeserialization(json); // Full deserialization of JSON string FullDeserializationOfString(jsonString); // Using selectors UsingSelectors(json); // Partial deserialization of JSON object PartialDeserialization(json); // How to get the value of JSON elements. GetValues(json); // How to go through all the children UsingForEach(json); // Dynamic generations of JSON DynamicGeneration(); }
public void LoadSettings(OnlineMapsJSONItem json) { // TODO: Implement }
public void Load(OnlineMapsJSONItem json) { (json as OnlineMapsJSONObject).DeserializeObject(this, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); }
public override object Deserialize(Type type) { if (_count == 0) { return(null); } if (type.IsArray) { Type elementType = type.GetElementType(); Array v = Array.CreateInstance(elementType, _count); if (array[0] is OnlineMapsJSONObject) { IEnumerable <MemberInfo> members = OnlineMapsReflectionHelper.GetMembers(elementType, BindingFlags.Instance | BindingFlags.Public); for (int i = 0; i < _count; i++) { OnlineMapsJSONItem child = array[i]; object item = (child as OnlineMapsJSONObject).Deserialize(elementType, members); v.SetValue(item, i); } } else { for (int i = 0; i < _count; i++) { OnlineMapsJSONItem child = array[i]; object item = child.Deserialize(elementType); v.SetValue(item, i); } } return(v); } if (OnlineMapsReflectionHelper.IsGenericType(type)) { Type listType = OnlineMapsReflectionHelper.GetGenericArguments(type)[0]; object v = Activator.CreateInstance(type); if (array[0] is OnlineMapsJSONObject) { IEnumerable <MemberInfo> members = OnlineMapsReflectionHelper.GetMembers(listType, BindingFlags.Instance | BindingFlags.Public); for (int i = 0; i < _count; i++) { OnlineMapsJSONItem child = array[i]; object item = (child as OnlineMapsJSONObject).Deserialize(listType, members); try { MethodInfo methodInfo = OnlineMapsReflectionHelper.GetMethod(type, "Add"); if (methodInfo != null) { methodInfo.Invoke(v, new[] { item }); } } catch { } } } else { for (int i = 0; i < _count; i++) { OnlineMapsJSONItem child = array[i]; object item = child.Deserialize(listType); try { MethodInfo methodInfo = OnlineMapsReflectionHelper.GetMethod(type, "Add"); if (methodInfo != null) { methodInfo.Invoke(v, new[] { item }); } } catch { } } } return(v); } return(null); }
/// <summary> /// Adds an element to the array. /// </summary> /// <param name="item">Element</param> public void Add(OnlineMapsJSONItem item) { array.Add(item); _count++; }
public void AddRange(OnlineMapsJSONItem collection) { AddRange(collection as OnlineMapsJSONArray); }
/// <summary> /// Adds element to the dictionary /// </summary> /// <param name="name">Key</param> /// <param name="value">Value</param> public void Add(string name, OnlineMapsJSONItem value) { _table[name] = value; }
public override object Deserialize(Type type) { if (_count == 0) { return(null); } if (type.IsArray) { Type elementType = type.GetElementType(); Array v = Array.CreateInstance(elementType, _count); if (array[0] is OnlineMapsJSONObject) { MemberInfo[] members = elementType.GetMembers(BindingFlags.Instance | BindingFlags.Public); for (int i = 0; i < _count; i++) { OnlineMapsJSONItem child = array[i]; object item = (child as OnlineMapsJSONObject).Deserialize(elementType, members); v.SetValue(item, i); } } else { for (int i = 0; i < _count; i++) { OnlineMapsJSONItem child = array[i]; object item = child.Deserialize(elementType); v.SetValue(item, i); } } return(v); } #if !NETFX_CORE if (type.IsGenericType) #else if (type.GetTypeInfo().IsGenericType) #endif { Type listType = type.GetGenericArguments()[0]; object v = Activator.CreateInstance(type); if (array[0] is OnlineMapsJSONObject) { MemberInfo[] members = listType.GetMembers(BindingFlags.Instance | BindingFlags.Public); for (int i = 0; i < _count; i++) { OnlineMapsJSONItem child = array[i]; object item = (child as OnlineMapsJSONObject).Deserialize(listType, members); try { type.GetMethod("Add").Invoke(v, new[] { item }); } catch { } } } else { for (int i = 0; i < _count; i++) { OnlineMapsJSONItem child = array[i]; object item = child.Deserialize(listType); try { type.GetMethod("Add").Invoke(v, new[] { item }); } catch { } } } return(v); } return(null); }