Example #1
0
            object ParseObject(Type type)
            {
                NodeTypeFieldData data = NodeTypeFieldData.GetNodeTypeFieldData(type);

                if (data == null)
                {
                    return(null);
                }
                object ret = Activator.CreateInstance(type);

                json.Read();
                while (true)
                {
                    switch (NextToken)
                    {
                    case TOKEN.NONE:
                        return(null);

                    case TOKEN.COMMA:
                        continue;

                    case TOKEN.CURLY_CLOSE:
                        return(ret);

                    default:
                        string name = ParseString(type_string);
                        if (name == null)
                        {
                            return(null);
                        }
                        if (NextToken != TOKEN.COLON)
                        {
                            return(null);
                        }
                        json.Read();
                        NodeTypeFieldData.FieldData field = data.GetField(name);
                        if (field.field == null)
                        {
                            ParseNext(null); break;
                        }
                        object value = ParseNext(field.field.FieldType);
                        if (field.field.FieldType.IsSubclassOf(type_data_node_base))
                        {
                            if (field.property == null)
                            {
                                break;
                            }
                        }
                        if (field.property != null)
                        {
                            field.property.SetValue(ret, value);
                        }
                        else
                        {
                            field.field.SetValue(ret, value);
                        }
                        break;
                    }
                }
            }
Example #2
0
        public static NodeTypeFieldData GetNodeTypeFieldData(Type type)
        {
            if (!type.IsSubclassOf(type_data_node_base))
            {
                return(null);
            }
            NodeTypeFieldData data;

            lock (node_types) {
                if (!node_types.TryGetValue(type, out data))
                {
                    data = new NodeTypeFieldData(type);
                    node_types.Add(type, data);
                }
            }
            return(data);
        }
Example #3
0
            void SerializeObject(DataNodeBase obj, bool list2dict, bool perfectPrint)
            {
                NodeTypeFieldData data = NodeTypeFieldData.GetNodeTypeFieldData(obj.GetType());

                NodeTypeFieldData.FieldData[] fields = data.GetFields();
                bool first = true;

                if (perfectPrint)
                {
                    builder.Append(indent);
                }
                builder.Append('{');
                indent = indent + "    ";

                foreach (NodeTypeFieldData.FieldData field in fields)
                {
                    if (!first)
                    {
                        builder.Append(',');
                    }
                    if (perfectPrint)
                    {
                        builder.AppendLine();
                        builder.Append(indent);
                    }
                    SerializeString(field.name);
                    builder.Append(':');
                    SerializeValue(false, field.field.GetValue(obj), list2dict, perfectPrint);
                    first = false;
                }
                indent = indent.Substring(4);
                if (perfectPrint)
                {
                    builder.AppendLine();
                    builder.Append(indent);
                }
                builder.Append('}');
            }