123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- /* INFINITY CODE 2013-2019 */
- /* http://www.infinity-code.com */
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Reflection;
- using System.Text;
- namespace InfinityCode.RealWorldTerrain.JSON
- {
- /// <summary>
- /// The base class of JSON elements.
- /// </summary>
- public abstract class RealWorldTerrainJsonItem : IEnumerable<RealWorldTerrainJsonItem>
- {
- /// <summary>
- /// Get the element by index
- /// </summary>
- /// <param name="index">Index of element</param>
- /// <returns>Element</returns>
- public abstract RealWorldTerrainJsonItem this[int index] { get; }
- /// <summary>
- /// Get the element by key.\n
- /// Supports XPath like selectors:\n
- /// ["key"] - get element by key.\n
- /// ["key1/key2"] - get element key2, which is a child of the element key1.\n
- /// ["key/N"] - where N is number. Get array element by index N, which is a child of the element key1.\n
- /// ["key/*"] - get all array elements, which is a child of the element key1.\n
- /// ["//key"] - get all elements with the key on the first or the deeper levels of the current element. \n
- /// </summary>
- /// <param name="key">Element key</param>
- /// <returns>Element</returns>
- public abstract RealWorldTerrainJsonItem this[string key] { get; }
- /// <summary>
- /// Serializes the object and adds to the current json node.
- /// </summary>
- /// <param name="obj">Object</param>
- /// <returns>Current json node</returns>
- public virtual RealWorldTerrainJsonItem AppendObject(object obj)
- {
- throw new Exception("AppendObject is only allowed for RealWorldTerrainJsonObject.");
- }
- /// <summary>
- /// Returns the value of the child element, converted to the specified type.
- /// </summary>
- /// <typeparam name="T">Type of variable</typeparam>
- /// <param name="childName">Child element key</param>
- /// <returns>Value</returns>
- public T ChildValue<T>(string childName)
- {
- RealWorldTerrainJsonItem el = this[childName];
- if (el == null) return default(T);
- return el.Value<T>();
- }
- /// <summary>
- /// Deserializes current element
- /// </summary>
- /// <typeparam name="T">Type</typeparam>
- /// <returns>Object</returns>
- public T Deserialize<T>(BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public)
- {
- return (T) Deserialize(typeof(T), bindingFlags);
- }
- /// <summary>
- /// Deserializes current element
- /// </summary>
- /// <param name="type">Type</param>
- /// <returns>Object</returns>
- public abstract object Deserialize(Type type, BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public);
- /// <summary>
- /// Get all elements with the key on the first or the deeper levels of the current element.
- /// </summary>
- /// <param name="key">Key</param>
- /// <returns>Elements</returns>
- public abstract RealWorldTerrainJsonItem GetAll(string key);
- /// <summary>
- /// Converts the current and the child elements to JSON string.
- /// </summary>
- /// <param name="b">StringBuilder instance</param>
- public abstract void ToJSON(StringBuilder b);
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- public virtual IEnumerator<RealWorldTerrainJsonItem> GetEnumerator()
- {
- return null;
- }
- public override string ToString()
- {
- StringBuilder b = new StringBuilder();
- ToJSON(b);
- return b.ToString();
- }
- /// <summary>
- /// Returns the value of the element, converted to the specified type.
- /// </summary>
- /// <param name="type">Type of variable</param>
- /// <returns>Value</returns>
- public abstract object Value(Type type);
- /// <summary>
- /// Returns the value of the element, converted to the specified type.
- /// </summary>
- /// <typeparam name="T">Type of variable</typeparam>
- /// <returns>Value</returns>
- public virtual T Value<T>()
- {
- return (T) Value(typeof(T));
- }
- /// <summary>
- /// Returns the value of the element, converted to the specified type.
- /// </summary>
- /// <typeparam name="T">Type of variable</typeparam>
- /// <returns>Value</returns>
- public T V<T>()
- {
- return Value<T>();
- }
- /// <summary>
- /// Returns the value of the child element, converted to the specified type.
- /// </summary>
- /// <typeparam name="T">Type of variable</typeparam>
- /// <param name="childName">Child element key</param>
- /// <returns>Value</returns>
- public T V<T>(string childName)
- {
- return ChildValue<T>(childName);
- }
- }
- }
|