public void Example()
        {
            #region Usage
            JTokenWriter writer = new JTokenWriter();
            writer.WriteStartObject();
            writer.WritePropertyName("name1");
            writer.WriteValue("value1");
            writer.WritePropertyName("name2");
            writer.WriteStartArray();
            writer.WriteValue(1);
            writer.WriteValue(2);
            writer.WriteEndArray();
            writer.WriteEndObject();

            JObject o = (JObject)writer.Token;

            Console.WriteLine(o.ToString());
            // {
            //   "name1": "value1",
            //   "name2": [
            //     1,
            //     2
            //   ]
            // }
            #endregion
        }
		private JToken CreateJObject (JsonReader reader)
		{
			ValidationUtils.ArgumentNotNull(reader, "reader");

			// this is needed because we've already read inside the object, looking for metadata properties
			using (JTokenWriter writer = new JTokenWriter()) {
				writer.WriteStartObject();

				do {
					if (reader.TokenType == JsonToken.PropertyName) {
						string propertyName = (string)reader.Value;
						do {
							if (!reader.Read())
								break;
						} while (reader.TokenType == JsonToken.Comment);

						if (CheckPropertyName(reader, propertyName))
							continue;

						writer.WritePropertyName(propertyName);
						writer.WriteToken(reader, true, true);
					}
					else if (reader.TokenType == JsonToken.Comment) {
						// eat
					}
					else {
						writer.WriteEndObject();
						return writer.Token;
					}
				} while (reader.Read());

				throw JsonSerializationException.Create(reader, "Unexpected end when deserializing object.");
			}
		}
예제 #3
0
        public void State()
        {
            using (JsonWriter jsonWriter = new JTokenWriter())
            {
                Assert.Equal(WriteState.Start, jsonWriter.WriteState);

                jsonWriter.WriteStartObject();
                Assert.Equal(WriteState.Object, jsonWriter.WriteState);

                jsonWriter.WritePropertyName("CPU");
                Assert.Equal(WriteState.Property, jsonWriter.WriteState);

                jsonWriter.WriteValue("Intel");
                Assert.Equal(WriteState.Object, jsonWriter.WriteState);

                jsonWriter.WritePropertyName("Drives");
                Assert.Equal(WriteState.Property, jsonWriter.WriteState);

                jsonWriter.WriteStartArray();
                Assert.Equal(WriteState.Array, jsonWriter.WriteState);

                jsonWriter.WriteValue("DVD read/writer");
                Assert.Equal(WriteState.Array, jsonWriter.WriteState);

#if NET40
                jsonWriter.WriteValue(new BigInteger(123));
                Assert.Equal(WriteState.Array, jsonWriter.WriteState);
#endif

                jsonWriter.WriteValue(new byte[0]);
                Assert.Equal(WriteState.Array, jsonWriter.WriteState);

                jsonWriter.WriteEnd();
                Assert.Equal(WriteState.Object, jsonWriter.WriteState);

                jsonWriter.WriteEndObject();
                Assert.Equal(WriteState.Start, jsonWriter.WriteState);
            }
        }