/* 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
{
///
/// The base class of JSON elements.
///
public abstract class RealWorldTerrainJsonItem : IEnumerable
{
///
/// Get the element by index
///
/// Index of element
/// Element
public abstract RealWorldTerrainJsonItem this[int index] { get; }
///
/// 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
///
/// Element key
/// Element
public abstract RealWorldTerrainJsonItem this[string key] { get; }
///
/// Serializes the object and adds to the current json node.
///
/// Object
/// Current json node
public virtual RealWorldTerrainJsonItem AppendObject(object obj)
{
throw new Exception("AppendObject is only allowed for RealWorldTerrainJsonObject.");
}
///
/// Returns the value of the child element, converted to the specified type.
///
/// Type of variable
/// Child element key
/// Value
public T ChildValue(string childName)
{
RealWorldTerrainJsonItem el = this[childName];
if (el == null) return default(T);
return el.Value();
}
///
/// Deserializes current element
///
/// Type
/// Object
public T Deserialize(BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public)
{
return (T) Deserialize(typeof(T), bindingFlags);
}
///
/// Deserializes current element
///
/// Type
/// Object
public abstract object Deserialize(Type type, BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public);
///
/// Get all elements with the key on the first or the deeper levels of the current element.
///
/// Key
/// Elements
public abstract RealWorldTerrainJsonItem GetAll(string key);
///
/// Converts the current and the child elements to JSON string.
///
/// StringBuilder instance
public abstract void ToJSON(StringBuilder b);
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public virtual IEnumerator GetEnumerator()
{
return null;
}
public override string ToString()
{
StringBuilder b = new StringBuilder();
ToJSON(b);
return b.ToString();
}
///
/// Returns the value of the element, converted to the specified type.
///
/// Type of variable
/// Value
public abstract object Value(Type type);
///
/// Returns the value of the element, converted to the specified type.
///
/// Type of variable
/// Value
public virtual T Value()
{
return (T) Value(typeof(T));
}
///
/// Returns the value of the element, converted to the specified type.
///
/// Type of variable
/// Value
public T V()
{
return Value();
}
///
/// Returns the value of the child element, converted to the specified type.
///
/// Type of variable
/// Child element key
/// Value
public T V(string childName)
{
return ChildValue(childName);
}
}
}