Esempio n. 1
0
        private fsResult TryParseObject(out fsData obj)
        {
            if (Character() != '{')
            {
                obj = null;
                return(MakeFailure("Expected initial { when parsing an object"));
            }

            // skip '{'
            if (TryMoveNext() == false)
            {
                obj = null;
                return(MakeFailure("Unexpected end of input when parsing an object"));
            }
            SkipSpace();

            var result = new Dictionary <string, fsData>(
                fsGlobalConfig.IsCaseSensitive ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase);

            while (HasValue() && Character() != '}')
            {
                fsResult failure;

                // parse the key
                SkipSpace();
                string key;
                failure = TryParseString(out key);
                if (failure.Failed)
                {
                    obj = null;
                    return(failure);
                }
                SkipSpace();

                // parse the ':' after the key
                if (HasValue() == false || Character() != ':' || TryMoveNext() == false)
                {
                    obj = null;
                    return(MakeFailure("Expected : after key \"" + key + "\""));
                }
                SkipSpace();

                // parse the value
                fsData value;
                failure = RunParse(out value);
                if (failure.Failed)
                {
                    obj = null;
                    return(failure);
                }

                result.Add(key, value);

                // parse the comma
                SkipSpace();
                if (HasValue() && Character() == ',')
                {
                    if (TryMoveNext() == false)
                    {
                        break;
                    }
                    SkipSpace();
                }
            }

            // skip the final }
            if (HasValue() == false || Character() != '}' || TryMoveNext() == false)
            {
                obj = null;
                return(MakeFailure("No closing } for object"));
            }

            obj = new fsData(result);
            return(fsResult.Success);
        }
Esempio n. 2
0
 /// <summary>
 /// Writes the compressed JSON output data to the given stream.
 /// </summary>
 /// <param name="data">The data to print.</param>
 /// <param name="outputStream">Where to write the printed data.</param>
 public static void CompressedJson(fsData data, StreamWriter outputStream)
 {
     BuildCompressedString(data, outputStream);
 }
Esempio n. 3
0
        /// <summary>
        /// Formats this data into the given builder.
        /// </summary>
        private static void BuildPrettyString(fsData data, TextWriter stream, int depth)
        {
            switch (data.Type)
            {
            case fsDataType.Null:
                stream.Write("null");
                break;

            case fsDataType.Boolean:
                if (data.AsBool)
                {
                    stream.Write("true");
                }
                else
                {
                    stream.Write("false");
                }
                break;

            case fsDataType.Double:
                stream.Write(ConvertDoubleToString(data.AsDouble));
                break;

            case fsDataType.Int64:
                stream.Write(data.AsInt64);
                break;

            case fsDataType.String:
                stream.Write('"');
                stream.Write(EscapeString(data.AsString));
                stream.Write('"');
                break;

            case fsDataType.Object: {
                stream.Write('{');
                stream.WriteLine();
                bool comma = false;
                foreach (var entry in data.AsDictionary)
                {
                    if (comma)
                    {
                        stream.Write(',');
                        stream.WriteLine();
                    }
                    comma = true;
                    InsertSpacing(stream, depth + 1);
                    stream.Write('"');
                    stream.Write(entry.Key);
                    stream.Write('"');
                    stream.Write(": ");
                    BuildPrettyString(entry.Value, stream, depth + 1);
                }
                stream.WriteLine();
                InsertSpacing(stream, depth);
                stream.Write('}');
                break;
            }

            case fsDataType.Array:
                // special case for empty lists; we don't put an empty line
                // between the brackets
                if (data.AsList.Count == 0)
                {
                    stream.Write("[]");
                }
                else
                {
                    bool comma = false;

                    stream.Write('[');
                    stream.WriteLine();
                    foreach (var entry in data.AsList)
                    {
                        if (comma)
                        {
                            stream.Write(',');
                            stream.WriteLine();
                        }
                        comma = true;
                        InsertSpacing(stream, depth + 1);
                        BuildPrettyString(entry, stream, depth + 1);
                    }
                    stream.WriteLine();
                    InsertSpacing(stream, depth);
                    stream.Write(']');
                }
                break;
            }
        }
Esempio n. 4
0
 /// <summary>
 /// Writes the pretty JSON output data to the given stream.
 /// </summary>
 /// <param name="data">The data to print.</param>
 /// <param name="outputStream">Where to write the printed data.</param>
 public static void PrettyJson(fsData data, TextWriter outputStream)
 {
     BuildPrettyString(data, outputStream, 0);
 }
Esempio n. 5
0
        private static void BuildCompressedString(fsData data, TextWriter stream)
        {
            switch (data.Type)
            {
            case fsDataType.Null:
                stream.Write("null");
                break;

            case fsDataType.Boolean:
                if (data.AsBool)
                {
                    stream.Write("true");
                }
                else
                {
                    stream.Write("false");
                }
                break;

            case fsDataType.Double:
                // doubles must *always* include a decimal
                stream.Write(ConvertDoubleToString(data.AsDouble));
                break;

            case fsDataType.Int64:
                stream.Write(data.AsInt64);
                break;

            case fsDataType.String:
                stream.Write('"');
                stream.Write(EscapeString(data.AsString));
                stream.Write('"');
                break;

            case fsDataType.Object: {
                stream.Write('{');
                bool comma = false;
                foreach (var entry in data.AsDictionary)
                {
                    if (comma)
                    {
                        stream.Write(',');
                    }
                    comma = true;
                    stream.Write('"');
                    stream.Write(entry.Key);
                    stream.Write('"');
                    stream.Write(":");
                    BuildCompressedString(entry.Value, stream);
                }
                stream.Write('}');
                break;
            }

            case fsDataType.Array: {
                stream.Write('[');
                bool comma = false;
                foreach (var entry in data.AsList)
                {
                    if (comma)
                    {
                        stream.Write(',');
                    }
                    comma = true;
                    BuildCompressedString(entry, stream);
                }
                stream.Write(']');
                break;
            }
            }
        }