diff --git a/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs index 7fcf1e57..5e036b13 100644 --- a/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs @@ -1308,19 +1308,19 @@ keyword such as type of business."" public void DefaultValueAttributeTest() { string json = JavaScriptConvert.SerializeObject(new DefaultValueAttributeTestClass(), - new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore }); + Formatting.None, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore }); Assert.AreEqual(@"{""TestField1"":0,""TestProperty1"":null}", json); json = JavaScriptConvert.SerializeObject(new DefaultValueAttributeTestClass { TestField1 = int.MinValue, TestProperty1 = "NotDefault" }, - new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore }); + Formatting.None, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore }); Assert.AreEqual(@"{""TestField1"":-2147483648,""TestProperty1"":""NotDefault""}", json); json = JavaScriptConvert.SerializeObject(new DefaultValueAttributeTestClass { TestField1 = 21, TestProperty1 = "NotDefault" }, - new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore }); + Formatting.None, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore }); Assert.AreEqual(@"{""TestProperty1"":""NotDefault""}", json); json = JavaScriptConvert.SerializeObject(new DefaultValueAttributeTestClass { TestField1 = 21, TestProperty1 = "TestProperty1Value" }, - new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore }); + Formatting.None, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore }); Assert.AreEqual(@"{}", json); } diff --git a/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs b/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs index 4e1250ae..f94ae92c 100644 --- a/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs @@ -383,5 +383,32 @@ namespace Newtonsoft.Json.Tests Assert.AreEqual("1", sw.ToString()); } + + [Test] + public void WriteRawValue() + { + StringBuilder sb = new StringBuilder(); + StringWriter sw = new StringWriter(sb); + + using (JsonWriter jsonWriter = new JsonTextWriter(sw)) + { + int i = 0; + string rawJson = "[1,2]"; + + jsonWriter.WriteStartObject(); + + while (i < 3) + { + jsonWriter.WritePropertyName("d" + i); + jsonWriter.WriteRawValue(rawJson); + + i++; + } + + jsonWriter.WriteEndObject(); + } + + Assert.AreEqual(@"{""d0"":[1,2],""d1"":[1,2],""d2"":[1,2]}", sb.ToString()); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs index ec88fcee..5b5af163 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JArrayTests.cs @@ -290,5 +290,13 @@ Parameter name: index")] Assert.AreEqual(j, v4.Parent); Assert.AreEqual(1, j.IndexOf(v4)); } + + [Test] + [ExpectedException(typeof(Exception), ExpectedMessage = "Error reading JArray from JsonReader. Current JsonReader item is not an array: StartObject")] + public void Parse_ShouldThrowOnUnexpectedToken() + { + string json = @"{""prop"":""value""}"; + JArray.Parse(json); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs index 9598be61..9cfab0f8 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JObjectTests.cs @@ -309,5 +309,13 @@ Parameter name: arrayIndex")] Assert.AreEqual("LastNameValue", raw.LastName); Assert.AreEqual("[1,2,3,4,5]", raw.RawContent.Content); } + + [Test] + [ExpectedException(typeof(Exception), ExpectedMessage = "Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray")] + public void Parse_ShouldThrowOnUnexpectedToken() + { + string json = @"[""prop""]"; + JObject.Parse(json); + } } } diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj index 816f93ef..946b30d3 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj @@ -23,6 +23,9 @@ v3.5 + false + + true diff --git a/Src/Newtonsoft.Json/DateTimeOffset.cs b/Src/Newtonsoft.Json/DateTimeOffset.cs index 5670fd05..601ea534 100644 --- a/Src/Newtonsoft.Json/DateTimeOffset.cs +++ b/Src/Newtonsoft.Json/DateTimeOffset.cs @@ -1,11 +1,17 @@ #if PocketPC +#pragma warning disable 1591 + +// This class is... borrowed from .NET and Microsoft for a short time. +// Hopefully Microsoft will add DateTimeOffset to the compact framework +// or I will rewrite a striped down version of this file myself + using System.Globalization; using System.Runtime.InteropServices; using System.Runtime.Serialization; using System.Security.Permissions; using System; -namespace Newtonsoft.Json +namespace System { public struct DateTimeOffset : IComparable, IFormattable, IComparable, IEquatable { @@ -312,26 +318,6 @@ namespace Newtonsoft.Json return new DateTimeOffset(time.Ticks, offset); } - //public override string ToString() - //{ - // return DateTimeFormat.Format(this.ClockDateTime, null, DateTimeFormatInfo.CurrentInfo, this.Offset); - //} - - //public string ToString(IFormatProvider formatProvider) - //{ - // return DateTimeFormat.Format(this.ClockDateTime, null, DateTimeFormatInfo.GetInstance(formatProvider), this.Offset); - //} - - //public string ToString(string format) - //{ - // return DateTimeFormat.Format(this.ClockDateTime, format, DateTimeFormatInfo.CurrentInfo, this.Offset); - //} - - //public string ToString(string format, IFormatProvider formatProvider) - //{ - // return DateTimeFormat.Format(this.ClockDateTime, format, DateTimeFormatInfo.GetInstance(formatProvider), this.Offset); - //} - public string ToString(string format, IFormatProvider formatProvider) { if (!string.IsNullOrEmpty(format)) diff --git a/Src/Newtonsoft.Json/JavaScriptConvert.cs b/Src/Newtonsoft.Json/JavaScriptConvert.cs index 14a86631..78ab6e51 100644 --- a/Src/Newtonsoft.Json/JavaScriptConvert.cs +++ b/Src/Newtonsoft.Json/JavaScriptConvert.cs @@ -436,7 +436,20 @@ namespace Newtonsoft.Json /// A JSON string representation of the object. public static string SerializeObject(object value) { - return SerializeObject(value, (JsonSerializerSettings)null); + return SerializeObject(value, Formatting.None, (JsonSerializerSettings)null); + } + + /// + /// Serializes the specified object to a JSON string. + /// + /// The object to serialize. + /// Indicates how the output is formatted. + /// + /// A JSON string representation of the object. + /// + public static string SerializeObject(object value, Formatting formatting) + { + return SerializeObject(value, formatting, (JsonSerializerSettings)null); } /// @@ -451,26 +464,28 @@ namespace Newtonsoft.Json ? new JsonSerializerSettings { Converters = converters } : null; - return SerializeObject(value, settings); + return SerializeObject(value, Formatting.None, settings); } /// /// Serializes the specified object to a JSON string using a collection of . /// /// The object to serialize. - /// - /// The used to serialize the object. - /// If this is null, default serialization settings will be is used. - /// - /// A JSON string representation of the object. - public static string SerializeObject(object value, JsonSerializerSettings settings) + /// Indicates how the output is formatted. + /// The used to serialize the object. + /// If this is null, default serialization settings will be is used. + /// + /// A JSON string representation of the object. + /// + public static string SerializeObject(object value, Formatting formatting, JsonSerializerSettings settings) { JsonSerializer jsonSerializer = JsonSerializer.Create(settings); StringWriter sw = new StringWriter(CultureInfo.InvariantCulture); - using (JsonWriter jsonWriter = new JsonTextWriter(sw)) + using (JsonTextWriter jsonWriter = new JsonTextWriter(sw)) { - //jsonWriter.Formatting = Formatting.Indented; + jsonWriter.Formatting = formatting; + jsonSerializer.Serialize(jsonWriter, value); } diff --git a/Src/Newtonsoft.Json/JsonSerializer.cs b/Src/Newtonsoft.Json/JsonSerializer.cs index 67cac60b..be80ea90 100644 --- a/Src/Newtonsoft.Json/JsonSerializer.cs +++ b/Src/Newtonsoft.Json/JsonSerializer.cs @@ -159,6 +159,11 @@ namespace Newtonsoft.Json } + /// + /// Creates a new instance using the specified objects. + /// + /// The settings. + /// public static JsonSerializer Create(JsonSerializerSettings settings) { JsonSerializer jsonSerializer = new JsonSerializer(); @@ -729,7 +734,7 @@ namespace Newtonsoft.Json } else if (value is JsonRaw) { - writer.WriteRaw(((JsonRaw)value).Content, true); + writer.WriteRawValue(((JsonRaw)value).Content); } else { diff --git a/Src/Newtonsoft.Json/JsonSerializerSettings.cs b/Src/Newtonsoft.Json/JsonSerializerSettings.cs index 4b4f7e54..f9d6ee18 100644 --- a/Src/Newtonsoft.Json/JsonSerializerSettings.cs +++ b/Src/Newtonsoft.Json/JsonSerializerSettings.cs @@ -6,6 +6,9 @@ using Newtonsoft.Json.Utilities; namespace Newtonsoft.Json { + /// + /// Specifies the settings on a object. + /// public class JsonSerializerSettings { internal const ReferenceLoopHandling DefaultReferenceLoopHandling = ReferenceLoopHandling.Error; @@ -14,13 +17,40 @@ namespace Newtonsoft.Json internal const DefaultValueHandling DefaultDefaultValueHandling = DefaultValueHandling.Include; internal const ObjectCreationHandling DefaultObjectCreationHandling = ObjectCreationHandling.Auto; + /// + /// Gets or sets how reference loops (e.g. a class referencing itself) is handled. + /// + /// Reference loop handling. public ReferenceLoopHandling ReferenceLoopHandling { get; set; } + /// + /// Gets or sets how missing members (e.g. JSON contains a property that isn't a member on the object) are handled during deserialization. + /// + /// Missing member handling. public MissingMemberHandling MissingMemberHandling { get; set; } + /// + /// Gets or sets how objects are created during deserialization. + /// + /// The object creation handling. public ObjectCreationHandling ObjectCreationHandling { get; set; } + /// + /// Gets or sets how null values are handled during serialization and deserialization. + /// + /// Null value handling. public NullValueHandling NullValueHandling { get; set; } + /// + /// Gets or sets how null default are handled during serialization and deserialization. + /// + /// The default value handling. public DefaultValueHandling DefaultValueHandling { get; set; } + /// + /// Gets or sets a collection that will be used during serialization. + /// + /// The converters. public IList Converters { get; set; } + /// + /// Initializes a new instance of the class. + /// public JsonSerializerSettings() { ReferenceLoopHandling = DefaultReferenceLoopHandling; diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs index 9f34cb43..7ebaf012 100644 --- a/Src/Newtonsoft.Json/JsonWriter.cs +++ b/Src/Newtonsoft.Json/JsonWriter.cs @@ -372,7 +372,7 @@ namespace Newtonsoft.Json WriteValue((DateTime)reader.Value); break; case JsonToken.Raw: - WriteRaw((string)reader.Value, true); + WriteRawValue((string)reader.Value); break; default: throw MiscellaneousUtils.CreateArgumentOutOfRangeException("TokenType", reader.TokenType, "Unexpected token type."); @@ -608,19 +608,22 @@ namespace Newtonsoft.Json } /// - /// Writes raw JSON. + /// Writes raw JSON without changing the writer's state. /// /// The raw JSON to write. public virtual void WriteRaw(string json) { } - internal void WriteRaw(string json, bool autoCompleteValue) + /// + /// Writes raw JSON where a value is expected and updates the writer's state. + /// + /// The raw JSON to write. + public virtual void WriteRawValue(string json) { WriteRaw(json); - // hack - if (autoCompleteValue) - AutoComplete(JsonToken.Undefined); + // hack. want writer to change state as if a value had been written + AutoComplete(JsonToken.Undefined); } /// diff --git a/Src/Newtonsoft.Json/Linq/JArray.cs b/Src/Newtonsoft.Json/Linq/JArray.cs index 0251d8e3..55dfd054 100644 --- a/Src/Newtonsoft.Json/Linq/JArray.cs +++ b/Src/Newtonsoft.Json/Linq/JArray.cs @@ -106,7 +106,7 @@ namespace Newtonsoft.Json.Linq } if (reader.TokenType != JsonToken.StartArray) { - throw new Exception("Current JsonReader item is not an object."); + throw new Exception("Error reading JArray from JsonReader. Current JsonReader item is not an array: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType)); } else { diff --git a/Src/Newtonsoft.Json/Linq/JObject.cs b/Src/Newtonsoft.Json/Linq/JObject.cs index 0ec0aa40..d0fd004d 100644 --- a/Src/Newtonsoft.Json/Linq/JObject.cs +++ b/Src/Newtonsoft.Json/Linq/JObject.cs @@ -193,7 +193,7 @@ namespace Newtonsoft.Json.Linq } if (reader.TokenType != JsonToken.StartObject) { - throw new Exception("Current JsonReader item is not an object."); + throw new Exception("Error reading JObject from JsonReader. Current JsonReader item is not an object: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType)); } else { diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.Compact.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.Compact.csproj index bcd60ada..082c18a0 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.Compact.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.Compact.csproj @@ -43,6 +43,7 @@ 512 4 Off + bin\Release\Newtonsoft.Json.Compact.XML diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.Silverlight.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.Silverlight.csproj index f9baa9f4..bff55878 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.Silverlight.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.Silverlight.csproj @@ -48,170 +48,11 @@ - - Converters\HtmlColorConverter.cs - - - Converters\IsoDateTimeConverter.cs - - - Converters\JavaScriptDateTimeConverter.cs - - - Converters\JsonDateTimeSerializationMode.cs - - - Converters\XmlNodeConverter.cs - - - JavaScriptConvert.cs - - - JsonConverter.cs - - - JsonConverterAttribute.cs - - - JsonConverterCollection.cs - - - JsonIgnoreAttribute.cs - - - JsonObjectAttribute.cs - - - JsonPropertyAttribute.cs - - - JsonReader.cs - - - JsonReaderException.cs - - - JsonSerializationException.cs - - - JsonSerializer.cs - - - JsonTextReader.cs - - - JsonTextWriter.cs - - - JsonToken.cs - - - JsonWriter.cs - - - JsonWriterException.cs - - - Linq\Extensions.cs - - - Linq\JArray.cs - - - Linq\JConstructor.cs - - - Linq\JContainer.cs - - - Linq\JEnumerable.cs - - - Linq\JObject.cs - - - Linq\JProperty.cs - - - Linq\JsonTokenReader.cs - - - Linq\JsonTokenType.cs - - - Linq\JsonTokenWriter.cs - - - Linq\JToken.cs - - - Linq\JTokenEqualityComparer.cs - - - Linq\JValue.cs - - - MemberMapping.cs - - - MemberMappingCollection.cs - - - MemberSerialization.cs - - - MissingMemberHandling.cs - - - NullValueHandling.cs - - - ObjectCreationHandling.cs - - - ReferenceLoopHandling.cs - - - StringBuffer.cs - - - Utilities\CollectionUtils.cs - - - Utilities\ConvertUtils.cs - - - Utilities\DateTimeUtils.cs - - - Utilities\DictionaryWrapper.cs - - - Utilities\JavaScriptUtils.cs - - - Utilities\ListWrapper.cs - - - Utilities\MathUtils.cs - - - Utilities\MiscellaneousUtils.cs - - - Utilities\ReflectionUtils.cs - - - Utilities\StringUtils.cs - - - Utilities\ValidationUtils.cs - - + diff --git a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs index 7adbec43..f8014098 100644 --- a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs @@ -27,16 +27,26 @@ using System.Security; // associated with an assembly. #if SILVERLIGHT [assembly: AssemblyTitle("Newtonsoft Json.NET Silverlight")] -[assembly: InternalsVisibleTo("Newtonsoft.Json.Tests.Silverlight")] #elif PocketPC [assembly: AssemblyTitle("Newtonsoft Json.NET Compact")] -[assembly: InternalsVisibleTo("Newtonsoft.Json.Tests.Compact")] #else [assembly: AssemblyTitle("Newtonsoft Json.NET")] -[assembly: InternalsVisibleTo("Newtonsoft.Json.Tests")] [assembly: AllowPartiallyTrustedCallers] #endif +#if !SIGNED + +#if SILVERLIGHT +[assembly: InternalsVisibleTo("Newtonsoft.Json.Tests.Silverlight")] +#elif PocketPC +[assembly: InternalsVisibleTo("Newtonsoft.Json.Tests.Compact")] +#else +[assembly: InternalsVisibleTo("Newtonsoft.Json.Tests")] +#endif + +#endif + + [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")]