Exemplo n.º 1
0
        public static JSONResult ToJSONResult(JSONDocument document)
        {
            JSONResult result = new JSONResult();

            result.Key     = document.Key;
            result._values = document.ToDictionary();
            return(result);
        }
Exemplo n.º 2
0
        public override void WriteJson(JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
        {
            if (IsSimpleType(value))
            {
                writer.WriteValue(value);
            }
            else if (value is JSONDocument.JsonObject)
            {
                writer.WriteRawValue(((JSONDocument.JsonObject)value).JsonString);
            }
            else if (value is Array || value is ArrayList)
            {
                writer.WriteRawValue(JsonConvert.SerializeObject(value));
            }
            else
            {
                writer.WriteStartObject();
                JSONDocument document = (JSONDocument)value;
                IDictionary <string, object> values = document.ToDictionary();

                foreach (KeyValuePair <string, object> pair in values)
                {
                    if (IsSimpleType(pair.Value))
                    {
                        writer.WritePropertyName(pair.Key);
                        serializer.Serialize(writer, pair.Value);
                    }
                    else if (pair.Value is IJSONDocument)
                    {
                        writer.WritePropertyName(pair.Key);
                        writer.WriteRawValue(JsonConvert.SerializeObject(pair.Value));
                    }
                    else if (pair.Value is JSONDocument.JsonObject)
                    {
                        writer.WritePropertyName(pair.Key);
                        writer.WriteRawValue(((JSONDocument.JsonObject)pair.Value).JsonString);
                    }
                    else if (pair.Value is Array || pair.Value is ArrayList)
                    {
                        writer.WritePropertyName(pair.Key);
                        writer.WriteRawValue(JsonConvert.SerializeObject(pair.Value));
                    }
                    else
                    {
                        throw new Exception(string.Format("Type {0} is not supported on JSONDocument", pair.Value.GetType().FullName));
                    }
                }

                writer.WriteEndObject();
            }
        }