-Renamed Identifier to JsonRaw

-Added support for reading, writing and serializing raw JSON
This commit is contained in:
JamesNK
2008-09-27 23:37:55 +00:00
parent 500440e04f
commit 5bb0c17eec
21 changed files with 416 additions and 93 deletions
+41
View File
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
namespace Newtonsoft.Json.Tests
{
public class JsonRawTests : TestFixtureBase
{
[Test]
public void Create()
{
JsonRaw raw = new JsonRaw("Test");
Assert.AreEqual("Test", raw.Content);
Assert.AreEqual("Test", raw.ToString());
}
[Test]
public void EqualsShouldEqualOnSameContent()
{
JsonRaw r1 = new JsonRaw("Test");
JsonRaw r2 = new JsonRaw("Test");
Assert.AreEqual(r1, r2);
Assert.AreNotEqual(r1, null);
Assert.IsFalse(r1.Equals(null));
}
[Test]
public void GetHashCodeShouldEqualOnSameContent()
{
JsonRaw r1 = new JsonRaw("Test");
JsonRaw r2 = new JsonRaw("Test");
JsonRaw r3 = new JsonRaw("Test1");
Assert.AreEqual(r1.GetHashCode(), r2.GetHashCode());
Assert.AreNotEqual(r1.GetHashCode(), r3.GetHashCode());
}
}
}
@@ -1335,5 +1335,68 @@ keyword such as type of business.""
json = JavaScriptConvert.SerializeObject((int?)1); json = JavaScriptConvert.SerializeObject((int?)1);
Assert.AreEqual("1", json); Assert.AreEqual("1", json);
} }
public class PersonRaw
{
private Guid _internalId;
private string _firstName;
private string _lastName;
private JsonRaw _rawContent;
[JsonIgnore]
public Guid InternalId
{
get { return _internalId; }
set { _internalId = value; }
}
[JsonProperty("first_name")]
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
public JsonRaw RawContent
{
get { return _rawContent; }
set { _rawContent = value; }
}
[JsonProperty("last_name")]
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
}
[Test]
public void SerializeJsonRaw()
{
PersonRaw personRaw = new PersonRaw
{
FirstName = "FirstNameValue",
RawContent = new JsonRaw("[1,2,3,4,5]"),
LastName = "LastNameValue"
};
string json;
json = JavaScriptConvert.SerializeObject(personRaw);
Assert.AreEqual(@"{""first_name"":""FirstNameValue"",""RawContent"":[1,2,3,4,5],""last_name"":""LastNameValue""}", json);
}
[Test]
public void DeserializeJsonRaw()
{
string json = @"{""first_name"":""FirstNameValue"",""RawContent"":[1,2,3,4,5],""last_name"":""LastNameValue""}";
PersonRaw personRaw = JavaScriptConvert.DeserializeObject<PersonRaw>(json);
Assert.AreEqual("FirstNameValue", personRaw.FirstName);
Assert.AreEqual("[1,2,3,4,5]", personRaw.RawContent.Content);
Assert.AreEqual("LastNameValue", personRaw.LastName);
}
} }
} }
@@ -280,6 +280,8 @@ namespace Newtonsoft.Json.Tests
jsonWriter.WriteValue(float.PositiveInfinity); jsonWriter.WriteValue(float.PositiveInfinity);
jsonWriter.WriteValue(float.NegativeInfinity); jsonWriter.WriteValue(float.NegativeInfinity);
jsonWriter.WriteEndArray(); jsonWriter.WriteEndArray();
jsonWriter.Flush();
} }
string expected = @"[ string expected = @"[
@@ -294,5 +296,92 @@ namespace Newtonsoft.Json.Tests
Assert.AreEqual(expected, result); Assert.AreEqual(expected, result);
} }
[Test]
public void WriteRawInStart()
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
using (JsonWriter jsonWriter = new JsonTextWriter(sw))
{
jsonWriter.Formatting = Formatting.Indented;
jsonWriter.WriteRaw("[1,2,3,4,5]");
jsonWriter.WriteWhitespace(" ");
jsonWriter.WriteStartArray();
jsonWriter.WriteValue(double.NaN);
jsonWriter.WriteEndArray();
}
string expected = @"[1,2,3,4,5] [
NaN
]";
string result = sb.ToString();
Assert.AreEqual(expected, result);
}
[Test]
public void WriteRawInArray()
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
using (JsonWriter jsonWriter = new JsonTextWriter(sw))
{
jsonWriter.Formatting = Formatting.Indented;
jsonWriter.WriteStartArray();
jsonWriter.WriteValue(double.NaN);
jsonWriter.WriteRaw(",[1,2,3,4,5]");
jsonWriter.WriteRaw(",[1,2,3,4,5]");
jsonWriter.WriteValue(float.NaN);
jsonWriter.WriteEndArray();
}
string expected = @"[
NaN,[1,2,3,4,5],[1,2,3,4,5],
NaN
]";
string result = sb.ToString();
Assert.AreEqual(expected, result);
}
[Test]
public void WriteRawInObject()
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
using (JsonWriter jsonWriter = new JsonTextWriter(sw))
{
jsonWriter.Formatting = Formatting.Indented;
jsonWriter.WriteStartObject();
jsonWriter.WriteRaw(@"""PropertyName"":[1,2,3,4,5]");
jsonWriter.WriteEnd();
}
string expected = @"{""PropertyName"":[1,2,3,4,5]}";
string result = sb.ToString();
Assert.AreEqual(expected, result);
}
[Test]
public void WriteToken()
{
JsonTextReader reader = new JsonTextReader(new StringReader("[1,2,3,4,5]"));
reader.Read();
reader.Read();
StringWriter sw = new StringWriter();
JsonTextWriter writer = new JsonTextWriter(sw);
writer.WriteToken(reader);
Assert.AreEqual("1", sw.ToString());
}
} }
} }
@@ -229,5 +229,85 @@ Parameter name: arrayIndex")]
((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(new KeyValuePair<string, JToken>[3], 1); ((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(new KeyValuePair<string, JToken>[3], 1);
} }
[Test]
public void FromObjectRaw()
{
JsonSerializerTest.PersonRaw raw = new JsonSerializerTest.PersonRaw
{
FirstName = "FirstNameValue",
RawContent = new JsonRaw("[1,2,3,4,5]"),
LastName = "LastNameValue"
};
JObject o = JObject.FromObject(raw);
Assert.AreEqual("FirstNameValue", (string)o["first_name"]);
Assert.AreEqual(JsonTokenType.Raw, ((JValue)o["RawContent"]).Type);
Assert.AreEqual("[1,2,3,4,5]", (string)o["RawContent"]);
Assert.AreEqual("LastNameValue", (string)o["last_name"]);
}
[Test]
public void JsonTokenReader()
{
JsonSerializerTest.PersonRaw raw = new JsonSerializerTest.PersonRaw
{
FirstName = "FirstNameValue",
RawContent = new JsonRaw("[1,2,3,4,5]"),
LastName = "LastNameValue"
};
JObject o = JObject.FromObject(raw);
JsonReader reader = new JsonTokenReader(o);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Raw, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.IsFalse(reader.Read());
}
[Test]
public void DeserializeFromRaw()
{
JsonSerializerTest.PersonRaw raw = new JsonSerializerTest.PersonRaw
{
FirstName = "FirstNameValue",
RawContent = new JsonRaw("[1,2,3,4,5]"),
LastName = "LastNameValue"
};
JObject o = JObject.FromObject(raw);
JsonReader reader = new JsonTokenReader(o);
JsonSerializer serializer = new JsonSerializer();
raw = (JsonSerializerTest.PersonRaw)serializer.Deserialize(reader, typeof(JsonSerializerTest.PersonRaw));
Assert.AreEqual("FirstNameValue", raw.FirstName);
Assert.AreEqual("LastNameValue", raw.LastName);
Assert.AreEqual("[1,2,3,4,5]", raw.RawContent.Content);
}
} }
} }
@@ -63,6 +63,7 @@
<Compile Include="Converters\XmlNodeConverterTest.cs" /> <Compile Include="Converters\XmlNodeConverterTest.cs" />
<Compile Include="DateTimeTestClass.cs" /> <Compile Include="DateTimeTestClass.cs" />
<Compile Include="JavaScriptConvertTest.cs" /> <Compile Include="JavaScriptConvertTest.cs" />
<Compile Include="JsonRawTests.cs" />
<Compile Include="JsonSerializerTest.cs" /> <Compile Include="JsonSerializerTest.cs" />
<Compile Include="JsonTextReaderTest.cs" /> <Compile Include="JsonTextReaderTest.cs" />
<Compile Include="JsonTextWriterTest.cs" /> <Compile Include="JsonTextWriterTest.cs" />
@@ -119,6 +119,7 @@
<Compile Include="Converters\XmlNodeConverterTest.cs" /> <Compile Include="Converters\XmlNodeConverterTest.cs" />
<Compile Include="DateTimeTestClass.cs" /> <Compile Include="DateTimeTestClass.cs" />
<Compile Include="JavaScriptConvertTest.cs" /> <Compile Include="JavaScriptConvertTest.cs" />
<Compile Include="JsonRawTests.cs" />
<Compile Include="JsonSerializerTest.cs" /> <Compile Include="JsonSerializerTest.cs" />
<Compile Include="JsonTextReaderTest.cs" /> <Compile Include="JsonTextReaderTest.cs" />
<Compile Include="JsonTextWriterTest.cs" /> <Compile Include="JsonTextWriterTest.cs" />
@@ -73,6 +73,7 @@
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="JsonRawTests.cs" />
<Compile Include="NullableDateTimeTestClass.cs" /> <Compile Include="NullableDateTimeTestClass.cs" />
<Compile Include="DateTimeTestClass.cs" /> <Compile Include="DateTimeTestClass.cs" />
<Compile Include="Converters\IsoDateTimeConverterTests.cs" /> <Compile Include="Converters\IsoDateTimeConverterTests.cs" />
@@ -31,37 +31,36 @@ using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Utilities;
namespace Newtonsoft.Json namespace Newtonsoft.Json
{ {
/// <summary> /// <summary>
/// /// Raw JSON content.
/// </summary> /// </summary>
public class Identifier public class JsonRaw
{ {
private string _name; private string _content;
/// <summary> /// <summary>
/// Gets the name. /// Gets the raw JSON string.
/// </summary> /// </summary>
/// <value>The name.</value> /// <value>The raw JSON string.</value>
public string Name public string Content
{ {
get { return _name; } get { return _content; }
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Identifier"/> class. /// Initializes a new instance of the <see cref="JsonRaw"/> class.
/// </summary> /// </summary>
/// <param name="name">The name.</param> /// <param name="content">The content.</param>
public Identifier(string name) public JsonRaw(string content)
{ {
_name = name; ValidationUtils.ArgumentNotNull(content, "content");
}
private static bool IsAsciiLetter(char c) _content = content;
{
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
} }
/// <summary> /// <summary>
@@ -74,36 +73,11 @@ namespace Newtonsoft.Json
/// <exception cref="T:System.NullReferenceException">The <paramref name="obj"/> parameter is null.</exception> /// <exception cref="T:System.NullReferenceException">The <paramref name="obj"/> parameter is null.</exception>
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
Identifier function = obj as Identifier; JsonRaw raw = obj as JsonRaw;
if (raw == null)
return false;
return Equals(function); return (string.Compare(_content, raw.Content, StringComparison.OrdinalIgnoreCase) == 0);
}
/// <summary>
/// Equalses the specified function.
/// </summary>
/// <param name="function">The function.</param>
/// <returns></returns>
public bool Equals(Identifier function)
{
return (_name == function.Name);
}
/// <summary>
/// Equalses the specified a.
/// </summary>
/// <param name="a">A.</param>
/// <param name="b">The b.</param>
/// <returns></returns>
public static bool Equals(Identifier a, Identifier b)
{
if (a == b)
return true;
if (a != null && b != null)
return a.Equals(b);
return false;
} }
/// <summary> /// <summary>
@@ -114,7 +88,7 @@ namespace Newtonsoft.Json
/// </returns> /// </returns>
public override int GetHashCode() public override int GetHashCode()
{ {
return _name.GetHashCode(); return _content.GetHashCode();
} }
/// <summary> /// <summary>
@@ -125,29 +99,23 @@ namespace Newtonsoft.Json
/// </returns> /// </returns>
public override string ToString() public override string ToString()
{ {
return _name; return _content;
} }
/// <summary> /// <summary>
/// Implements the operator ==. /// Creates an instance of <see cref="JsonRaw"/> with the content of the reader's current token.
/// </summary> /// </summary>
/// <param name="a">A.</param> /// <param name="reader">The reader.</param>
/// <param name="b">The b.</param> /// <returns>An instance of <see cref="JsonRaw"/> with the content of the reader's current token.</returns>
/// <returns>The result of the operator.</returns> public static JsonRaw Create(JsonReader reader)
public static bool operator ==(Identifier a, Identifier b)
{ {
return Identifier.Equals(a, b); using (StringWriter sw = new StringWriter(CultureInfo.InvariantCulture))
} using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
{
jsonWriter.WriteToken(reader);
/// <summary> return new JsonRaw(sw.ToString());
/// Implements the operator !=. }
/// </summary>
/// <param name="a">A.</param>
/// <param name="b">The b.</param>
/// <returns>The result of the operator.</returns>
public static bool operator !=(Identifier a, Identifier b)
{
return !Identifier.Equals(a, b);
} }
} }
} }
+2
View File
@@ -276,6 +276,7 @@ namespace Newtonsoft.Json
case JsonToken.Null: case JsonToken.Null:
case JsonToken.Date: case JsonToken.Date:
case JsonToken.String: case JsonToken.String:
case JsonToken.Raw:
_currentState = State.PostValue; _currentState = State.PostValue;
break; break;
} }
@@ -351,6 +352,7 @@ namespace Newtonsoft.Json
case JsonToken.EndArray: case JsonToken.EndArray:
case JsonToken.EndConstructor: case JsonToken.EndConstructor:
case JsonToken.Date: case JsonToken.Date:
case JsonToken.Raw:
return false; return false;
default: default:
throw MiscellaneousUtils.CreateArgumentOutOfRangeException("token", token, "Unexpected JsonToken value."); throw MiscellaneousUtils.CreateArgumentOutOfRangeException("token", token, "Unexpected JsonToken value.");
+6 -2
View File
@@ -262,6 +262,10 @@ namespace Newtonsoft.Json
{ {
return converter.ReadJson(reader, objectType); return converter.ReadJson(reader, objectType);
} }
else if (objectType == typeof(JsonRaw))
{
return JsonRaw.Create(reader);
}
else else
{ {
switch (reader.TokenType) switch (reader.TokenType)
@@ -723,9 +727,9 @@ namespace Newtonsoft.Json
{ {
SerializeEnumerable(writer, (IEnumerable)value); SerializeEnumerable(writer, (IEnumerable)value);
} }
else if (value is Identifier) else if (value is JsonRaw)
{ {
writer.WriteRaw(value.ToString()); writer.WriteRaw(((JsonRaw)value).Content, true);
} }
else else
{ {
+6 -6
View File
@@ -259,14 +259,14 @@ namespace Newtonsoft.Json
} }
/// <summary> /// <summary>
/// Writes raw JavaScript manually. /// Writes raw JSON.
/// </summary> /// </summary>
/// <param name="javaScript">The raw JavaScript to write.</param> /// <param name="json">The raw JSON to write.</param>
public override void WriteRaw(string javaScript) public override void WriteRaw(string json)
{ {
base.WriteRaw(javaScript); base.WriteRaw(json);
// hack. some 'raw' or 'other' token perhaps?
WriteValueInternal(javaScript, JsonToken.Undefined); _writer.Write(json);
} }
/// <summary> /// <summary>
+4
View File
@@ -59,6 +59,10 @@ namespace Newtonsoft.Json
/// </summary> /// </summary>
Comment, Comment,
/// <summary> /// <summary>
/// Raw JSON.
/// </summary>
Raw,
/// <summary>
/// An interger. /// An interger.
/// </summary> /// </summary>
Integer, Integer,
+37 -7
View File
@@ -115,6 +115,7 @@ namespace Newtonsoft.Json
/* StartConstructor */{ State.ConstructorStart, State.ConstructorStart, State.Error, State.Error, State.ConstructorStart, State.ConstructorStart, State.ConstructorStart, State.ConstructorStart, State.Error, State.Error }, /* StartConstructor */{ State.ConstructorStart, State.ConstructorStart, State.Error, State.Error, State.ConstructorStart, State.ConstructorStart, State.ConstructorStart, State.ConstructorStart, State.Error, State.Error },
/* StartProperty */{ State.Property, State.Error, State.Property, State.Property, State.Error, State.Error, State.Error, State.Error, State.Error, State.Error }, /* StartProperty */{ State.Property, State.Error, State.Property, State.Property, State.Error, State.Error, State.Error, State.Error, State.Error, State.Error },
/* Comment */{ State.Start, State.Property, State.ObjectStart, State.Object, State.ArrayStart, State.Array, State.Constructor, State.Constructor, State.Error, State.Error }, /* Comment */{ State.Start, State.Property, State.ObjectStart, State.Object, State.ArrayStart, State.Array, State.Constructor, State.Constructor, State.Error, State.Error },
/* Raw */{ State.Start, State.Property, State.ObjectStart, State.Object, State.ArrayStart, State.Array, State.Constructor, State.Constructor, State.Error, State.Error },
/* Value */{ State.Start, State.Object, State.Error, State.Error, State.Array, State.Array, State.Constructor, State.Constructor, State.Error, State.Error }, /* Value */{ State.Start, State.Object, State.Error, State.Error, State.Array, State.Array, State.Constructor, State.Constructor, State.Error, State.Error },
}; };
@@ -309,7 +310,14 @@ namespace Newtonsoft.Json
{ {
ValidationUtils.ArgumentNotNull(reader, "reader"); ValidationUtils.ArgumentNotNull(reader, "reader");
int currentDepth = (reader.TokenType == JsonToken.None) ? -1 : reader.Depth; int currentDepth;
if (reader.TokenType == JsonToken.None)
currentDepth = -1;
else if (!IsStartToken(reader.TokenType))
currentDepth = reader.Depth + 1;
else
currentDepth = reader.Depth;
do do
{ {
@@ -363,6 +371,9 @@ namespace Newtonsoft.Json
case JsonToken.Date: case JsonToken.Date:
WriteValue((DateTime)reader.Value); WriteValue((DateTime)reader.Value);
break; break;
case JsonToken.Raw:
WriteRaw((string)reader.Value, true);
break;
default: default:
throw MiscellaneousUtils.CreateArgumentOutOfRangeException("TokenType", reader.TokenType, "Unexpected token type."); throw MiscellaneousUtils.CreateArgumentOutOfRangeException("TokenType", reader.TokenType, "Unexpected token type.");
} }
@@ -386,6 +397,19 @@ namespace Newtonsoft.Json
} }
} }
private bool IsStartToken(JsonToken token)
{
switch (token)
{
case JsonToken.StartObject:
case JsonToken.StartArray:
case JsonToken.StartConstructor:
return true;
default:
return false;
}
}
private void WriteEnd(JsonTokenType type) private void WriteEnd(JsonTokenType type)
{ {
switch (type) switch (type)
@@ -536,7 +560,7 @@ namespace Newtonsoft.Json
case JsonToken.Undefined: case JsonToken.Undefined:
case JsonToken.Date: case JsonToken.Date:
// a value is being written // a value is being written
token = 6; token = 7;
break; break;
} }
@@ -584,13 +608,19 @@ namespace Newtonsoft.Json
} }
/// <summary> /// <summary>
/// Writes raw JavaScript manually. /// Writes raw JSON.
/// </summary> /// </summary>
/// <param name="javaScript">The raw JavaScript to write.</param> /// <param name="json">The raw JSON to write.</param>
public virtual void WriteRaw(string javaScript) public virtual void WriteRaw(string json)
{ {
// hack. some 'raw' or 'other' token perhaps? }
AutoComplete(JsonToken.Undefined);
internal void WriteRaw(string json, bool autoCompleteValue)
{
WriteRaw(json);
// hack
if (autoCompleteValue)
AutoComplete(JsonToken.Undefined);
} }
/// <summary> /// <summary>
+1 -1
View File
@@ -377,7 +377,7 @@ namespace Newtonsoft.Json.Linq
private static bool ValidateString(JToken o) private static bool ValidateString(JToken o)
{ {
return (o.Type == JsonTokenType.String || IsNullable(o)); return (o.Type == JsonTokenType.String || o.Type == JsonTokenType.Comment || o.Type == JsonTokenType.Raw || IsNullable(o));
} }
private static string GetType(JToken t) private static string GetType(JToken t)
+31 -1
View File
@@ -188,12 +188,23 @@ namespace Newtonsoft.Json.Linq
return new JValue(value, JsonTokenType.String); return new JValue(value, JsonTokenType.String);
} }
/// <summary>
/// Creates a <see cref="JValue"/> of raw JSON with the given value.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>A <see cref="JValue"/> of raw JSON with the given value.</returns>
public static JValue CreateRaw(string value)
{
return new JValue(value, JsonTokenType.Raw);
}
private static JsonTokenType GetValueType(JsonTokenType? current, object value) private static JsonTokenType GetValueType(JsonTokenType? current, object value)
{ {
if (value == null) if (value == null)
return JsonTokenType.Null; return JsonTokenType.Null;
else if (value is string) else if (value is string)
return (current == JsonTokenType.Comment) ? JsonTokenType.Comment : JsonTokenType.String; return GetStringValueType(current);
else if (value is long || value is int || value is short || value is sbyte else if (value is long || value is int || value is short || value is sbyte
|| value is ulong || value is uint || value is ushort || value is byte) || value is ulong || value is uint || value is ushort || value is byte)
return JsonTokenType.Integer; return JsonTokenType.Integer;
@@ -209,6 +220,22 @@ namespace Newtonsoft.Json.Linq
throw new ArgumentException("Could not determin JSON object type for type {0}.".FormatWith(CultureInfo.InvariantCulture, value.GetType())); throw new ArgumentException("Could not determin JSON object type for type {0}.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
} }
private static JsonTokenType GetStringValueType(JsonTokenType? current)
{
if (current == null)
return JsonTokenType.String;
switch (current.Value)
{
case JsonTokenType.Comment:
case JsonTokenType.String:
case JsonTokenType.Raw:
return current.Value;
default:
return JsonTokenType.String;
}
}
/// <summary> /// <summary>
/// Gets the node type for this <see cref="JToken"/>. /// Gets the node type for this <see cref="JToken"/>.
/// </summary> /// </summary>
@@ -281,6 +308,9 @@ namespace Newtonsoft.Json.Linq
writer.WriteValue(Convert.ToDateTime(v, CultureInfo.InvariantCulture)); writer.WriteValue(Convert.ToDateTime(v, CultureInfo.InvariantCulture));
}, _value); }, _value);
break; break;
case JsonTokenType.Raw:
writer.WriteRaw(_value.ToString());
break;
case JsonTokenType.Null: case JsonTokenType.Null:
writer.WriteNull(); writer.WriteNull();
break; break;
@@ -164,6 +164,9 @@ namespace Newtonsoft.Json.Linq
case JsonTokenType.Date: case JsonTokenType.Date:
SetToken(JsonToken.Date, ((JValue)token).Value); SetToken(JsonToken.Date, ((JValue)token).Value);
break; break;
case JsonTokenType.Raw:
SetToken(JsonToken.Raw, ((JValue)token).Value);
break;
default: default:
throw MiscellaneousUtils.CreateArgumentOutOfRangeException("Type", token.Type, "Unexpected JsonTokenType."); throw MiscellaneousUtils.CreateArgumentOutOfRangeException("Type", token.Type, "Unexpected JsonTokenType.");
} }
+5 -1
View File
@@ -86,6 +86,10 @@ namespace Newtonsoft.Json.Linq
/// <summary> /// <summary>
/// A date value. /// A date value.
/// </summary> /// </summary>
Date Date,
/// <summary>
/// A raw JSON value.
/// </summary>
Raw
} }
} }
+12 -7
View File
@@ -128,7 +128,12 @@ namespace Newtonsoft.Json.Linq
private void AddValue(object value, JsonToken token) private void AddValue(object value, JsonToken token)
{ {
_parent.Add(new JValue(value)); AddValue(new JValue(value), token);
}
private void AddValue(JValue value, JsonToken token)
{
_parent.Add(value);
if (_parent.Type == JsonTokenType.Property) if (_parent.Type == JsonTokenType.Property)
_parent = _parent.Parent; _parent = _parent.Parent;
@@ -154,14 +159,14 @@ namespace Newtonsoft.Json.Linq
} }
/// <summary> /// <summary>
/// Writes raw JavaScript manually. /// Writes raw JSON.
/// </summary> /// </summary>
/// <param name="javaScript">The raw JavaScript to write.</param> /// <param name="json">The raw JSON to write.</param>
public override void WriteRaw(string javaScript) public override void WriteRaw(string json)
{ {
base.WriteRaw(javaScript); base.WriteRaw(json);
// hack. some 'raw' or 'other' token perhaps? // hack
AddValue(javaScript, JsonToken.Undefined); AddValue(JValue.CreateRaw(json), JsonToken.Raw);
} }
/// <summary> /// <summary>
@@ -61,7 +61,6 @@
<Compile Include="Converters\XmlNodeConverter.cs" /> <Compile Include="Converters\XmlNodeConverter.cs" />
<Compile Include="DateTimeOffset.cs" /> <Compile Include="DateTimeOffset.cs" />
<Compile Include="DefaultValueHandling.cs" /> <Compile Include="DefaultValueHandling.cs" />
<Compile Include="Identifier.cs" />
<Compile Include="JavaScriptConvert.cs" /> <Compile Include="JavaScriptConvert.cs" />
<Compile Include="JsonConverter.cs" /> <Compile Include="JsonConverter.cs" />
<Compile Include="JsonConverterAttribute.cs" /> <Compile Include="JsonConverterAttribute.cs" />
@@ -69,6 +68,7 @@
<Compile Include="JsonIgnoreAttribute.cs" /> <Compile Include="JsonIgnoreAttribute.cs" />
<Compile Include="JsonObjectAttribute.cs" /> <Compile Include="JsonObjectAttribute.cs" />
<Compile Include="JsonPropertyAttribute.cs" /> <Compile Include="JsonPropertyAttribute.cs" />
<Compile Include="JsonRaw.cs" />
<Compile Include="JsonReader.cs" /> <Compile Include="JsonReader.cs" />
<Compile Include="JsonReaderException.cs" /> <Compile Include="JsonReaderException.cs" />
<Compile Include="JsonSerializationException.cs" /> <Compile Include="JsonSerializationException.cs" />
@@ -63,9 +63,6 @@
<Compile Include="..\Newtonsoft.Json\Converters\XmlNodeConverter.cs"> <Compile Include="..\Newtonsoft.Json\Converters\XmlNodeConverter.cs">
<Link>Converters\XmlNodeConverter.cs</Link> <Link>Converters\XmlNodeConverter.cs</Link>
</Compile> </Compile>
<Compile Include="..\Newtonsoft.Json\Identifier.cs">
<Link>Identifier.cs</Link>
</Compile>
<Compile Include="..\Newtonsoft.Json\JavaScriptConvert.cs"> <Compile Include="..\Newtonsoft.Json\JavaScriptConvert.cs">
<Link>JavaScriptConvert.cs</Link> <Link>JavaScriptConvert.cs</Link>
</Compile> </Compile>
@@ -216,7 +213,6 @@
<Compile Include="Converters\JsonDateTimeSerializationMode.cs" /> <Compile Include="Converters\JsonDateTimeSerializationMode.cs" />
<Compile Include="Converters\XmlNodeConverter.cs" /> <Compile Include="Converters\XmlNodeConverter.cs" />
<Compile Include="DefaultValueHandling.cs" /> <Compile Include="DefaultValueHandling.cs" />
<Compile Include="Identifier.cs" />
<Compile Include="JavaScriptConvert.cs" /> <Compile Include="JavaScriptConvert.cs" />
<Compile Include="JsonConverter.cs" /> <Compile Include="JsonConverter.cs" />
<Compile Include="JsonConverterAttribute.cs" /> <Compile Include="JsonConverterAttribute.cs" />
@@ -224,6 +220,7 @@
<Compile Include="JsonIgnoreAttribute.cs" /> <Compile Include="JsonIgnoreAttribute.cs" />
<Compile Include="JsonObjectAttribute.cs" /> <Compile Include="JsonObjectAttribute.cs" />
<Compile Include="JsonPropertyAttribute.cs" /> <Compile Include="JsonPropertyAttribute.cs" />
<Compile Include="JsonRaw.cs" />
<Compile Include="JsonReader.cs" /> <Compile Include="JsonReader.cs" />
<Compile Include="JsonReaderException.cs" /> <Compile Include="JsonReaderException.cs" />
<Compile Include="JsonSerializationException.cs" /> <Compile Include="JsonSerializationException.cs" />
+1 -1
View File
@@ -93,7 +93,7 @@
<Compile Include="JsonTextWriter.cs" /> <Compile Include="JsonTextWriter.cs" />
<Compile Include="JsonWriterException.cs" /> <Compile Include="JsonWriterException.cs" />
<Compile Include="JsonReaderException.cs" /> <Compile Include="JsonReaderException.cs" />
<Compile Include="Identifier.cs" /> <Compile Include="JsonRaw.cs" />
<Compile Include="JsonConverter.cs" /> <Compile Include="JsonConverter.cs" />
<Compile Include="JsonConverterCollection.cs" /> <Compile Include="JsonConverterCollection.cs" />
<Compile Include="JsonReader.cs" /> <Compile Include="JsonReader.cs" />