예제 #1
0
        private static JsonPrimitive ProcessData(JNode node)
        {
            JsonPrimitive result = null;

            if (!node.IsData())
            {
                throw new Exception("Hjson serializer error: Unknown JNode type.");
            }

            var dataType = node.GetDataType();

            if (dataType == typeof(string))
            {
                result = new JsonPrimitive(node.AsData <string>().Value);
            }
            else if (dataType == typeof(bool))
            {
                result = new JsonPrimitive(node.AsData <bool>().Value);
            }
            else if (dataType == typeof(byte))
            {
                result = new JsonPrimitive(node.AsData <byte>().Value);
            }
            else if (dataType == typeof(sbyte))
            {
                result = new JsonPrimitive(node.AsData <sbyte>().Value);
            }
            else if (dataType == typeof(char))
            {
                result = new JsonPrimitive(node.AsData <char>().Value);
            }
            else if (dataType == typeof(float))
            {
                result = new JsonPrimitive(node.AsData <float>().Value);
            }
            else if (dataType == typeof(double))
            {
                result = new JsonPrimitive(node.AsData <double>().Value);
            }
            else if (dataType == typeof(decimal))
            {
                result = new JsonPrimitive(node.AsData <decimal>().Value);
            }
            else if (dataType == typeof(short))
            {
                result = new JsonPrimitive(node.AsData <short>().Value);
            }
            else if (dataType == typeof(int))
            {
                result = new JsonPrimitive(node.AsData <int>().Value);
            }
            else if (dataType == typeof(long))
            {
                result = new JsonPrimitive(node.AsData <long>().Value);
            }
            else if (dataType == typeof(ushort))
            {
                result = new JsonPrimitive(node.AsData <ushort>().Value);
            }
            else if (dataType == typeof(uint))
            {
                result = new JsonPrimitive(node.AsData <uint>().Value);
            }
            else if (dataType == typeof(ulong))
            {
                result = new JsonPrimitive(Cast.This(node.AsData <ulong>().Value).AsString());
            }
            else if (dataType.IsEnum)
            {
                result = new JsonPrimitive(node.GetRawData().ToString());
            }
            else if (DataTypeConverter.HasConverter(new ConversionType(dataType, typeof(string))))
            {
                result = new JsonPrimitive((string)DataTypeConverter.GetConverter(new ConversionType(dataType, typeof(string)))(node.GetRawData()));
            }
            else
            {
                Log.Warning($"No string converter found for the type {dataType.GetFriendlyName()}, falling back to default ToString() method");

                result = new JsonPrimitive(node.GetRawData().ToString());
            }

            return(result);
        }