示例#1
0
        public static JsonObjectNode CreateIdentityCard(string name)
        {
            var card = new JsonObjectNode();

            card["Name"] = new JsonStringNode(name);
            card["Age"]  = new JsonIntegerNode(22);
            return(card);
        }
示例#2
0
        public void ToObject_Null()
        {
            // Arrange
            var  stringNode = new JsonStringNode();
            Type type       = null;

            // Act
            stringNode.ConvertTo(type);
        }
示例#3
0
        public void ToObject_TrueToBoolean()
        {
            // Arrange
            var stringNode = new JsonStringNode("true");

            // Act
            bool value = stringNode.ConvertTo <bool>();

            // Assert
            Assert.AreEqual(true, value);
        }
示例#4
0
        public void ToObject_NumberStringToInt32()
        {
            // Arrange
            var stringNode = new JsonStringNode("42");

            // Act
            int value = stringNode.ConvertTo <int>();

            // Assert
            Assert.AreEqual(42, value);
        }
示例#5
0
        public void ToObject_NumberStringToString()
        {
            // Arrange
            var stringNode = new JsonStringNode("42");

            // Act
            string value = stringNode.ConvertTo <string>();

            // Assert
            Assert.AreEqual("42", value);
        }
示例#6
0
        public void ToString_EscapeSequences()
        {
            // Arrange
            var stringNode = new JsonStringNode("Hello\r\n\tWorld!");

            // Act
            string result = stringNode.ToString();

            // Assert
            Assert.AreEqual("\"Hello\\r\\n\\tWorld!\"", result);
        }
示例#7
0
        public void ToString_SimpleCharacters()
        {
            // Arrange
            var stringNode = new JsonStringNode("Hello World!");

            // Act
            string result = stringNode.ToString();

            // Assert
            Assert.AreEqual("\"Hello World!\"", result);
        }
示例#8
0
 /// <inheritdoc />
 public override void Write(Utf8JsonWriter writer, DateTime?value, JsonSerializerOptions options)
 {
     if (value.HasValue)
     {
         writer.WriteStringValue(JsonStringNode.ToJson(value.Value, true));
     }
     else
     {
         writer.WriteNullValue();
     }
 }
示例#9
0
    /// <summary>
    /// Gets the YAML format string of the value.
    /// </summary>
    /// <returns>A YAML format string.</returns>
    public virtual string ToYamlString()
    {
        var str = new StringBuilder();

        foreach (var prop in ListExtensions.ToGroups(this))
        {
            if (prop.Key == null)
            {
                continue;
            }
            str.Append(prop.Key.IndexOfAny(StringExtensions.YamlSpecialChars) >= 0
                ? JsonStringNode.ToJson(prop.Key)
                : prop.Key);
            str.Append(": ");
            var values = prop.ToList();
            if (values == null || values.Count == 0)
            {
                str.AppendLine("!!null null");
                continue;
            }

            if (values.Count == 1)
            {
                var item = values[0];
                if (item == null)
                {
                    str.AppendLine("!!null null");
                    continue;
                }

                str.AppendLine(item.Length == 0 || item.Length > 100 || item.IndexOfAny(StringExtensions.YamlSpecialChars) >= 0
                    ? JsonStringNode.ToJson(item)
                    : item);
                continue;
            }

            foreach (var item in values)
            {
                str.AppendLine();
                str.Append("- ");
                if (item == null)
                {
                    str.AppendLine("!!null null");
                    continue;
                }

                str.AppendLine(item.IndexOfAny(StringExtensions.YamlSpecialChars) >= 0
                    ? JsonStringNode.ToJson(item)
                    : item);
            }
        }

        return(str.ToString());
    }
示例#10
0
        public void Value_set_SimpleCharacters()
        {
            // Arrange
            var stringNode = new JsonStringNode();

            // Act
            stringNode.Value = "Hello World!";

            // Assert
            Assert.AreEqual("Hello World!", stringNode.Value);
        }
示例#11
0
        public void ToString_Null()
        {
            // Arrange
            var stringNode = new JsonStringNode(null);

            // Act
            string result = stringNode.ToString();

            // Assert
            Assert.AreEqual("\"\"", result);
        }
示例#12
0
        public void Value_set_Empty()
        {
            // Arrange
            var stringNode = new JsonStringNode();

            // Act
            stringNode.Value = "";

            // Assert
            Assert.AreEqual("", stringNode.Value);
        }
示例#13
0
        public void WriteTo_SimpleCharacters()
        {
            // Arrange
            var stringNode = new JsonStringNode("Hello World!");
            var writer     = JsonWriter.Create();

            // Act
            stringNode.Write(writer);

            // Assert
            Assert.AreEqual("\"Hello World!\"", writer.ToString());
        }
示例#14
0
        public void WriteTo_Empty()
        {
            // Arrange
            var stringNode = new JsonStringNode("");
            var writer     = JsonWriter.Create();

            // Act
            stringNode.Write(writer);

            // Assert
            Assert.AreEqual("\"\"", writer.ToString());
        }
示例#15
0
        public void Clone_SimpleCharacters()
        {
            // Arrange
            var stringNode = new JsonStringNode("Hello World!");

            // Act
            var cloneNode = stringNode.Clone() as JsonStringNode;

            // Assert
            Assert.AreNotSame(stringNode, cloneNode);
            Assert.AreEqual(stringNode.Value, cloneNode.Value);
        }
示例#16
0
        public void Clone_Null()
        {
            // Arrange
            var stringNode = new JsonStringNode(null);

            // Act
            var cloneNode = stringNode.Clone() as JsonStringNode;

            // Assert
            Assert.AreNotSame(stringNode, cloneNode);
            Assert.AreEqual(stringNode.Value, cloneNode.Value);
        }
示例#17
0
        public void WriteTo_EscapeSequences()
        {
            // Arrange
            var stringNode = new JsonStringNode("Hello\r\n\tWorld!");
            var writer     = JsonWriter.Create();

            // Act
            stringNode.Write(writer);

            // Assert
            Assert.AreEqual("\"Hello\\r\\n\\tWorld!\"", writer.ToString());
        }
        private JsonStringNode ProduceJsonString(JsonTokenizer tokenizer)
        {
            JsonStringNode jString = null;

            try {
                JsonToken t     = tokenizer.Next();
                string    value = (string)t.Value;
                jString = (JsonStringNode)jsonTypeFactory.CreateString(value);
            } catch (Exception e) {
                throw new UnknownParserException("Failed to create a String node.", e, GetTailCharStream(tokenizer), PeekCharStream(tokenizer));
            }
            return(jString);
        }
示例#19
0
 /// <inheritdoc />
 public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
 {
     writer.WriteStringValue(JsonStringNode.ToJson(value, true));
 }