TinySite.Commands.ParseDocumentCommand.TryParseComplexValue C# (CSharp) Method

TryParseComplexValue() private static method

private static TryParseComplexValue ( string value, string content, int index, int &endOfLine, dynamic &complexValue ) : bool
value string
content string
index int
endOfLine int
complexValue dynamic
return bool
        private static bool TryParseComplexValue(string value, string content, int index, ref int endOfLine, out dynamic complexValue)
        {
            complexValue = null;

            if (value.StartsWith("[") || value.StartsWith("{"))
            {
                var openCharacter = value[0];
                var closeCharacter = (openCharacter == '[') ? ']' : '}';
                var countOpenCharacters = 1;

                var startJsonIndex = index;

                for (index = index + 1; countOpenCharacters > 0 && index < content.Length; ++index)
                {
                    var c = content[index];

                    // TODO: take into account string content and do not count brackets/braces
                    // inside strings.
                    if (c == openCharacter)
                    {
                        ++countOpenCharacters;
                    }
                    else if (c == closeCharacter)
                    {
                        --countOpenCharacters;
                    }
                }

                if (countOpenCharacters == 0)
                {
                    var json = content.Substring(startJsonIndex, index - startJsonIndex);

                    complexValue = CaseInsensitiveExpando.FromJson(json);

                    endOfLine = content.IndexOf('\n', index + 1);

                    if (endOfLine < 0)
                    {
                        endOfLine = content.Length;
                    }
                }
            }

            return complexValue != null;
        }