Exemplo n.º 1
0
        public override void VisitMap(PMap value)
        {
            outputBuilder.Append(JsonSpecialCharacter.CurlyOpenCharacter);
            currentDepth++;

            bool first = true;

            foreach (var kv in value)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    outputBuilder.Append(JsonSpecialCharacter.CommaCharacter);
                }

                AppendString(kv.Key);
                outputBuilder.Append(JsonSpecialCharacter.ColonCharacter);
                Visit(kv.Value);
            }

            currentDepth--;
            outputBuilder.Append(JsonSpecialCharacter.CurlyCloseCharacter);
        }
Exemplo n.º 2
0
        public static string ConvertToJson(PMap map)
        {
            var writer = new CompactSettingWriter();

            writer.Visit(map);
            return(writer.outputBuilder.ToString());
        }
Exemplo n.º 3
0
        public override void VisitMap(PMap value)
        {
            outputBuilder.Append(JsonSpecialCharacter.CurlyOpenCharacter);
            currentDepth++;

            bool first = true;

            foreach (var kv in value)
            {
                bool extraNewLineBeforeComment = !first;

                if (!first)
                {
                    outputBuilder.Append(JsonSpecialCharacter.CommaCharacter);
                }
                else
                {
                    first = false;
                }

                outputBuilder.AppendLine();

                string name = kv.Key;
                if ((options & SettingWriterOptions.SuppressSettingComments) == 0 &&
                    schema.TryGetProperty(new SettingKey(name), out SettingProperty property))
                {
                    if (extraNewLineBeforeComment)
                    {
                        outputBuilder.AppendLine();
                    }
                    AppendCommentLines(property.Description);
                }

                // This assumes that all default setting values fit on one line.
                AppendIndent();
                if ((options & SettingWriterOptions.CommentOutProperties) != 0)
                {
                    outputBuilder.Append(JsonSpecialCharacter.SingleLineCommentStart);
                }

                AppendString(name);
                outputBuilder.Append(JsonSpecialCharacter.ColonCharacter);
                outputBuilder.Append(SpaceChar);
                Visit(kv.Value);
            }

            currentDepth--;

            if (!first)
            {
                // Do this after decreasing currentDepth, so the closing bracket
                // is in the same x-position as the opening bracket.
                outputBuilder.AppendLine();
                AppendIndent();
            }

            outputBuilder.Append(JsonSpecialCharacter.CurlyCloseCharacter);
        }
Exemplo n.º 4
0
        public static string ConvertToJson(PMap map, SettingSchema schema, SettingWriterOptions options)
        {
            SettingWriter writer = new SettingWriter(schema, options);

            writer.Visit(map);

            // End files with a newline character.
            writer.outputBuilder.AppendLine();
            return(writer.outputBuilder.ToString());
        }
Exemplo n.º 5
0
        internal override Union <ITypeErrorBuilder, PValue> TryCreateFromMap(
            string json,
            GreenJsonMapSyntax jsonMapSyntax,
            out SettingObject convertedValue,
            int mapSyntaxStartPosition,
            List <JsonErrorInfo> errors)
        {
            var mapBuilder = new Dictionary <string, PValue>();

            // Analyze values with this schema while building the PMap.
            foreach (var(keyNodeStart, keyNode, valueNodeStart, valueNode) in jsonMapSyntax.ValidKeyValuePairs())
            {
                if (TryGetProperty(new SettingKey(keyNode.Value), out SettingProperty property))
                {
                    var valueOrError = property.TryCreateValue(
                        json,
                        valueNode,
                        mapSyntaxStartPosition + valueNodeStart,
                        errors);

                    if (valueOrError.IsOption2(out PValue convertedItemValue))
                    {
                        mapBuilder.Add(keyNode.Value, convertedItemValue);
                    }
                    else
                    {
                        ITypeErrorBuilder typeError = valueOrError.ToOption1();
                        errors.Add(ValueTypeErrorAtPropertyKey.Create(
                                       typeError,
                                       keyNode,
                                       valueNode,
                                       json,
                                       mapSyntaxStartPosition + keyNodeStart,
                                       mapSyntaxStartPosition + valueNodeStart));
                    }
                }
                else
                {
                    errors.Add(UnrecognizedPropertyKeyTypeError.Create(
                                   keyNode,
                                   json,
                                   mapSyntaxStartPosition + keyNodeStart));
                }
            }

            var map = new PMap(mapBuilder);

            convertedValue = new SettingObject(this, map);
            return(map);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Compares this <see cref="PMap"/> with another and returns if they are equal.
        /// </summary>
        /// <param name="other">
        /// The <see cref="PMap"/> to compare with.
        /// </param>
        /// <returns>
        /// true if both <see cref="PMap"/> instances are equal; otherwise false.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="other"/> is null.
        /// </exception>
        public bool EqualTo(PMap other)
        {
            if (other == null)
            {
                throw new ArgumentNullException(nameof(other));
            }

            // Compare Count properties for a fast exit if they are different.
            if (map.Count != other.map.Count)
            {
                return(false);
            }

            // Both key sets need to match exactly, but if Counts are equal a unidirectional check is sufficient.
            PValueEqualityComparer eq = PValueEqualityComparer.Instance;

            return(map.All(kv => other.map.TryGetValue(kv.Key, out PValue otherValue) &&
                           eq.AreEqual(kv.Value, otherValue)));
        }
Exemplo n.º 7
0
 internal SettingObject(SettingSchema schema, PMap map)
 {
     Schema = schema;
     Map    = map;
 }
Exemplo n.º 8
0
 public virtual void VisitMap(PMap value) => DefaultVisit(value);
Exemplo n.º 9
0
 public override Maybe <SettingObject> TryConvertFromMap(PMap map)
 => new SettingObject(this, map);
Exemplo n.º 10
0
 public abstract Maybe <T> TryConvertFromMap(PMap map);