当前位置: 首页>>代码示例>>C#>>正文


C# JsonTextWriter.WriteNull方法代码示例

本文整理汇总了C#中Newtonsoft.Json.JsonTextWriter.WriteNull方法的典型用法代码示例。如果您正苦于以下问题:C# JsonTextWriter.WriteNull方法的具体用法?C# JsonTextWriter.WriteNull怎么用?C# JsonTextWriter.WriteNull使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Newtonsoft.Json.JsonTextWriter的用法示例。


在下文中一共展示了JsonTextWriter.WriteNull方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ValueFormatting

    public void ValueFormatting()
    {
      StringBuilder sb = new StringBuilder();
      StringWriter sw = new StringWriter(sb);

      using (JsonWriter jsonWriter = new JsonTextWriter(sw))
      {
        jsonWriter.WriteStartArray();
        jsonWriter.WriteValue('@');
        jsonWriter.WriteValue("\r\n\t\f\b?{\\r\\n\"\'");
        jsonWriter.WriteValue(true);
        jsonWriter.WriteValue(10);
        jsonWriter.WriteValue(10.99);
        jsonWriter.WriteValue(0.99);
        jsonWriter.WriteValue(0.000000000000000001d);
        jsonWriter.WriteValue(0.000000000000000001m);
        jsonWriter.WriteValue((string)null);
        jsonWriter.WriteValue((object)null);
        jsonWriter.WriteValue("This is a string.");
        jsonWriter.WriteNull();
        jsonWriter.WriteUndefined();
        jsonWriter.WriteEndArray();
      }

      string expected = @"[""@"",""\r\n\t\f\b?{\\r\\n\""'"",true,10,10.99,0.99,1E-18,0.000000000000000001,null,null,""This is a string."",null,undefined]";
      string result = sb.ToString();

      Console.WriteLine("ValueFormatting");
      Console.WriteLine(result);

      Assert.AreEqual(expected, result);
    }
开发者ID:Gert-Cominotto,项目名称:MvvmCross,代码行数:32,代码来源:JsonTextWriterTest.cs

示例2: SerializeMessage

        public static void SerializeMessage(JsonTextWriter writer, ServiceType type, object message)
        {
            Require.NotNull(writer, "writer");
            Require.NotNull(type, "type");
            Require.NotNull(message, "message");

            writer.WriteStartObject();

            foreach (var field in type.Fields.Values)
            {
                writer.WritePropertyName(field.Tag.ToString(CultureInfo.InvariantCulture));

                object value = field.Getter(message);

                if (value == null)
                {
                    writer.WriteNull();
                }
                else if (field.CollectionType != null)
                {
                    writer.WriteStartArray();

                    foreach (object item in (IEnumerable)value)
                    {
                        if (field.ServiceType != null)
                            SerializeMessage(writer, field.ServiceType, item);
                        else
                            writer.WriteValue(item);
                    }

                    writer.WriteEndArray();
                }
                else
                {
                    if (field.ServiceType != null)
                        SerializeMessage(writer, field.ServiceType, value);
                    else
                        writer.WriteValue(value);
                }
            }

            writer.WriteEndObject();
        }
开发者ID:pvginkel,项目名称:ProtoChannel,代码行数:43,代码来源:JsonUtil.cs

示例3: WriteReadWrite

    public void WriteReadWrite()
    {
      StringBuilder sb = new StringBuilder();
      StringWriter sw = new StringWriter(sb);

      using (JsonWriter jsonWriter = new JsonTextWriter(sw)
        {
          Formatting = Formatting.Indented
        })
      {
        jsonWriter.WriteStartArray();
        jsonWriter.WriteValue(true);

        jsonWriter.WriteStartObject();
        jsonWriter.WritePropertyName("integer");
        jsonWriter.WriteValue(99);
        jsonWriter.WritePropertyName("string");
        jsonWriter.WriteValue("how now brown cow?");
        jsonWriter.WritePropertyName("array");

        jsonWriter.WriteStartArray();
        for (int i = 0; i < 5; i++)
        {
          jsonWriter.WriteValue(i);
        }

        jsonWriter.WriteStartObject();
        jsonWriter.WritePropertyName("decimal");
        jsonWriter.WriteValue(990.00990099m);
        jsonWriter.WriteEndObject();

        jsonWriter.WriteValue(5);
        jsonWriter.WriteEndArray();

        jsonWriter.WriteEndObject();

        jsonWriter.WriteValue("This is a string.");
        jsonWriter.WriteNull();
        jsonWriter.WriteNull();
        jsonWriter.WriteEndArray();
      }

      string json = sb.ToString();

      JsonSerializer serializer = new JsonSerializer();

      object jsonObject = serializer.Deserialize(new JsonTextReader(new StringReader(json)));

      sb = new StringBuilder();
      sw = new StringWriter(sb);

      using (JsonWriter jsonWriter = new JsonTextWriter(sw)
        {
          Formatting = Formatting.Indented
        })
      {
        serializer.Serialize(jsonWriter, jsonObject);
      }

      Assert.AreEqual(json, sb.ToString());
    }
开发者ID:robertherber,项目名称:PersonalTasks,代码行数:61,代码来源:JsonTextReaderTest.cs

示例4: Path

    public void Path()
    {
      StringBuilder sb = new StringBuilder();
      StringWriter sw = new StringWriter(sb);

      string text = "Hello world.";
      byte[] data = Encoding.UTF8.GetBytes(text);

      using (JsonTextWriter writer = new JsonTextWriter(sw))
      {
        writer.Formatting = Formatting.Indented;

        writer.WriteStartArray();
        Assert.AreEqual("", writer.Path);
        writer.WriteStartObject();
        Assert.AreEqual("[0]", writer.Path);
        writer.WritePropertyName("Property1");
        Assert.AreEqual("[0].Property1", writer.Path);
        writer.WriteStartArray();
        Assert.AreEqual("[0].Property1", writer.Path);
        writer.WriteValue(1);
        Assert.AreEqual("[0].Property1[0]", writer.Path);
        writer.WriteStartArray();
        Assert.AreEqual("[0].Property1[1]", writer.Path);
        writer.WriteStartArray();
        Assert.AreEqual("[0].Property1[1][0]", writer.Path);
        writer.WriteStartArray();
        Assert.AreEqual("[0].Property1[1][0][0]", writer.Path);
        writer.WriteEndObject();
        Assert.AreEqual("[0]", writer.Path);
        writer.WriteStartObject();
        Assert.AreEqual("[1]", writer.Path);
        writer.WritePropertyName("Property2");
        Assert.AreEqual("[1].Property2", writer.Path);
        writer.WriteStartConstructor("Constructor1");
        Assert.AreEqual("[1].Property2", writer.Path);
        writer.WriteNull();
        Assert.AreEqual("[1].Property2[0]", writer.Path);
        writer.WriteStartArray();
        Assert.AreEqual("[1].Property2[1]", writer.Path);
        writer.WriteValue(1);
        Assert.AreEqual("[1].Property2[1][0]", writer.Path);
        writer.WriteEnd();
        Assert.AreEqual("[1].Property2[1]", writer.Path);
        writer.WriteEndObject();
        Assert.AreEqual("[1]", writer.Path);
        writer.WriteEndArray();
        Assert.AreEqual("", writer.Path);
      }

      Assert.AreEqual(@"[
  {
    ""Property1"": [
      1,
      [
        [
          []
        ]
      ]
    ]
  },
  {
    ""Property2"": new Constructor1(
      null,
      [
        1
      ]
    )
  }
]", sb.ToString());
    }
开发者ID:RedBanies3ofThem,项目名称:tradelink.afterlife,代码行数:71,代码来源:JsonTextWriterTest.cs

示例5: XmlToJson


//.........这里部分代码省略.........
                                JTW.WriteStartObject();
                                break;
                            case ("arr"):
                                JTW.WriteStartArray();
                                break;
                            case("cons" ):
                                JTW.WriteStartConstructor("");
                                break;
                            case ("num"):
                            case ("str"):
                            case ("bool"):
                            case ("undef" ):
                                ValType = XR.Name;
                                Read = XR.Read();
                                NextRead = true;
                                if (XR.NodeType == XmlNodeType.EndElement)
                                {
                                    JTW.WriteValue("");
                                }
                                break;
                            case("prop"):
                                JTW.WritePropertyName(XR.GetAttribute("oname"));
                                break;
                        }
                        break;
                    case (XmlNodeType.EndElement ):
                        switch(XR.Name)
                        {
                            case("obj"):
                                JTW.WriteEndObject();
                                break;
                            case("arr"):
                                JTW.WriteEndArray();
                                break;
                            case("cons" ):
                                JTW.WriteEndConstructor();
                                break;
                        }
                        break;
                    case(XmlNodeType.Text):
                        switch(ValType)
                        {
                            case("num"):
                                try
                                {
                                    JTW.WriteValue(Int32.Parse(XR.Value.Trim()));
                                }
                                catch
                                {
                                    try
                                    {
                                        JTW.WriteValue(float.Parse(XR.Value.Trim()));
                                    }
                                    catch
                                    {
                                        JTW.WriteValue(XR.Value);
                                    }
                                }
                                break;
                            case("str"):
                                JTW.WriteValue(XR.Value.ToString());
                                break;
                            case("bool"):
                                if (XR.Value.ToString().Equals("1"))
                                {
                                    JTW.WriteValue(true);
                                }
                                else if(XR.Value.ToString().Equals("0"))
                                {
                                    JTW.WriteValue(false);
                                }
                                else
                                {
                                    JTW.WriteValue(XR.Value);
                                }
                                break;
                            case("undef"):
                                if (XR.Value.ToString() == "null")
                                {
                                    JTW.WriteNull();
                                }
                                else
                                {
                                    JTW.WriteValue(XR.Value.ToString());
                                }
                                break;
                        }
                        break;
                }
            }
            JTW.Close();
            if (InArray)
            {
                return string.Format("[{0}]",JW.ToString().Trim());
            }
            else
            {
                return JW.ToString().Trim();
            }
        }
开发者ID:0ks3ii,项目名称:IronWASP,代码行数:101,代码来源:JsonFormatPlugin.cs

示例6: CreatePlaylist_BuildJson

        private void CreatePlaylist_BuildJson(string title, string description, IEnumerable<Song> songs, Stream stream)
        {
            using (var streamWriter = new StreamWriter(stream))
            using (var jsonWriter = new JsonTextWriter(streamWriter))
            {
                jsonWriter.WriteStartArray(); // [

                // ["sessionId",1]
                jsonWriter.WriteStartArray();
                jsonWriter.WriteValue(this.SessionId);
                jsonWriter.WriteValue(1);
                jsonWriter.WriteEndArray();

                // [null, "Name", "Description",
                jsonWriter.WriteStartArray();
                jsonWriter.WriteNull();
                jsonWriter.WriteValue(title);
                jsonWriter.WriteValue(description);

                // [["Song-Guid", (int)songType], [...]]
                jsonWriter.WriteStartArray();
                if (songs != null)
                {
                    foreach (Song song in songs)
                    {
                        jsonWriter.WriteStartArray();
                        jsonWriter.WriteValue(song.ID);
                        jsonWriter.WriteValue(song.Type);
                        jsonWriter.WriteEndArray();
                    }
                }
                jsonWriter.WriteEndArray();

                jsonWriter.WriteEndArray(); //]
                jsonWriter.WriteEndArray(); //]
            }
        }
开发者ID:RubiksKid,项目名称:.NET-Google-Music-API,代码行数:37,代码来源:WebClient.cs

示例7: DumpOpCodes

		private static void DumpOpCodes()
		{
			using (var output = new StreamWriter(@"C:\tsv\pets\vmjs\src\runtime\opcodes.js"))
			using (var writer = new JsonTextWriter(output)
				{
					Formatting = Formatting.Indented
				})
			{
				var fields = typeof(OpCodes).GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.GetField);
				var opCodes = (from fi in fields
				               select new {fi.Name, OpCode = (OpCode) fi.GetValue(null)})
					.OrderBy(x => (uint) x.OpCode.Value)
					.ToArray();

				Action<string, object> prop = (name, value) =>
					{
						writer.WritePropertyName(name);
						writer.WriteValue(value);
					};

				Action<string, string> propEnum = (name, value) =>
					{
						writer.WritePropertyName(name);
						writer.WriteRawValue(value);
					};

				Action<OpCode> dump = opCode =>
					{
						writer.WriteStartObject();
						prop("name", opCode.Name);
						prop("size", opCode.Size);
						prop("value", opCode.Value);
						propEnum("operand", "OperandType." + opCode.OperandType);
						propEnum("flow", "FlowControl." + opCode.FlowControl);
						propEnum("type", "OpCodeType." + opCode.OpCodeType);
						propEnum("pop", "StackBehaviour." + opCode.StackBehaviourPop);
						propEnum("push", "StackBehaviour." + opCode.StackBehaviourPush);
						writer.WriteEndObject();
					};

				var shortOpCodes = opCodes.Where(x => x.OpCode.Size == 1).ToDictionary(x => (int) x.OpCode.Value, x => x.Name);
				var longOpCodes = opCodes.Where(x => x.OpCode.Size > 1).ToDictionary(x => x.OpCode.Value & 0xff, x => x.Name);

				writer.WriteRaw("// warning: this file was auto generated!\n");
				writer.WriteRaw("var OpCodes = ");
				writer.WriteStartObject();
				foreach (var f in opCodes)
				{
					writer.WritePropertyName(f.Name);
					dump(f.OpCode);
				}
				writer.WriteEndObject();
				writer.WriteRaw(";\n");

				writer.WriteRaw("var ShortOpCodes = ");
				writer.WriteStartArray();
				for (var i = 0; i <= 255; i++)
				{
					string name;
					if (shortOpCodes.TryGetValue(i, out name))
					{
						writer.WriteRawValue("OpCodes." + name);
					}
					else
					{
						writer.WriteNull();
					}
				}
				writer.WriteEndArray();
				writer.WriteRaw(";\n");

				var maxLongOpCode = longOpCodes.Keys.Max();
				writer.WriteRawValue("var LongOpCodes = ");
				writer.WriteStartArray();
				for (var i = 0; i <= maxLongOpCode; i++)
				{
					string name;
					if (longOpCodes.TryGetValue(i, out name))
					{
						writer.WriteRawValue("OpCodes." + name);
					}
					else
					{
						writer.WriteNull();
					}
				}
				writer.WriteEndArray();
				writer.WriteRaw(";\n");
			}
		}
开发者ID:sergeyt,项目名称:cil.js,代码行数:90,代码来源:Program.cs


注:本文中的Newtonsoft.Json.JsonTextWriter.WriteNull方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。