This commit is contained in:
2024-04-27 09:23:34 +02:00
commit 11e713ca6f
11884 changed files with 3263371 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
#region License
/*---------------------------------------------------------------------------------*\
Distributed under the terms of an MIT-style license:
The MIT License
Copyright (c) 2006-2009 Stephen M. McKamey
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
\*---------------------------------------------------------------------------------*/
#endregion License
using System;
namespace JsonFx.Json
{
/// <summary>
/// Allows classes to control their own JSON serialization
/// </summary>
public interface IJsonSerializable
{
void ReadJson(JsonReader reader);
void WriteJson(JsonWriter writer);
}
}

View File

@@ -0,0 +1,111 @@
#region License
/*---------------------------------------------------------------------------------*\
Distributed under the terms of an MIT-style license:
The MIT License
Copyright (c) 2006-2009 Stephen M. McKamey
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
\*---------------------------------------------------------------------------------*/
#endregion License
using System;
using System.Reflection;
using System.Xml.Serialization;
namespace JsonFx.Json
{
/// <summary>
/// Designates a property or field to not be serialized.
/// </summary>
[AttributeUsage(AttributeTargets.All, AllowMultiple=false)]
public sealed class JsonIgnoreAttribute : Attribute
{
#region Methods
/// <summary>
/// Gets a value which indicates if should be ignored in Json serialization.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool IsJsonIgnore(object value)
{
if (value == null)
{
return false;
}
Type type = value.GetType();
ICustomAttributeProvider provider = null;
if (type.IsEnum)
{
provider = type.GetField(Enum.GetName(type, value));
}
else
{
provider = value as ICustomAttributeProvider;
}
if (provider == null)
{
throw new ArgumentException();
}
return provider.IsDefined(typeof(JsonIgnoreAttribute), true);
}
/// <summary>
/// Gets a value which indicates if should be ignored in Json serialization.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool IsXmlIgnore(object value)
{
if (value == null)
{
return false;
}
Type type = value.GetType();
ICustomAttributeProvider provider = null;
if (type.IsEnum)
{
provider = type.GetField(Enum.GetName(type, value));
}
else
{
provider = value as ICustomAttributeProvider;
}
if (provider == null)
{
throw new ArgumentException();
}
return provider.IsDefined(typeof(XmlIgnoreAttribute), true);
}
#endregion Methods
}
}

View File

@@ -0,0 +1,178 @@
#region License
/*---------------------------------------------------------------------------------*\
Distributed under the terms of an MIT-style license:
The MIT License
Copyright (c) 2006-2009 Stephen M. McKamey
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
\*---------------------------------------------------------------------------------*/
#endregion License
using System;
using System.Reflection;
using System.Xml.Serialization;
namespace JsonFx.Json
{
/// <summary>
/// Specifies the naming to use for a property or field when serializing
/// </summary>
[AttributeUsage(AttributeTargets.All, AllowMultiple=false)]
public class JsonNameAttribute : Attribute
{
#region Fields
private string jsonName = null;
#endregion Fields
#region Init
/// <summary>
/// Ctor
/// </summary>
public JsonNameAttribute()
{
}
/// <summary>
/// Ctor
/// </summary>
/// <param name="jsonName"></param>
public JsonNameAttribute(string jsonName)
{
this.jsonName = JsonWriter.EnsureValidIdentifier(jsonName, false);
}
#endregion Init
#region Properties
/// <summary>
/// Gets and sets the name to be used in JSON
/// </summary>
public string Name
{
get { return this.jsonName; }
set { this.jsonName = JsonWriter.EnsureValidIdentifier(value, false); }
}
#endregion Properties
#region Methods
/// <summary>
/// Gets the name specified for use in Json serialization.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string GetJsonName(object value)
{
if (value == null)
{
return null;
}
Type type = value.GetType();
MemberInfo memberInfo = null;
if (type.IsEnum)
{
string name = Enum.GetName(type, value);
if (String.IsNullOrEmpty(name))
{
return null;
}
memberInfo = type.GetField(name);
}
else
{
memberInfo = value as MemberInfo;
}
if (memberInfo == null)
{
throw new ArgumentException();
}
if (!Attribute.IsDefined(memberInfo, typeof(JsonNameAttribute)))
{
return null;
}
JsonNameAttribute attribute = (JsonNameAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(JsonNameAttribute));
return attribute.Name;
}
///// <summary>
///// Gets the name specified for use in Json serialization.
///// </summary>
///// <param name="value"></param>
///// <returns></returns>
//public static string GetXmlName(object value)
//{
// if (value == null)
// {
// return null;
// }
// Type type = value.GetType();
// ICustomAttributeProvider memberInfo = null;
// if (type.IsEnum)
// {
// string name = Enum.GetName(type, value);
// if (String.IsNullOrEmpty(name))
// {
// return null;
// }
// memberInfo = type.GetField(name);
// }
// else
// {
// memberInfo = value as ICustomAttributeProvider;
// }
// if (memberInfo == null)
// {
// throw new ArgumentException();
// }
// XmlAttributes xmlAttributes = new XmlAttributes(memberInfo);
// if (xmlAttributes.XmlElements.Count == 1 &&
// !String.IsNullOrEmpty(xmlAttributes.XmlElements[0].ElementName))
// {
// return xmlAttributes.XmlElements[0].ElementName;
// }
// if (xmlAttributes.XmlAttribute != null &&
// !String.IsNullOrEmpty(xmlAttributes.XmlAttribute.AttributeName))
// {
// return xmlAttributes.XmlAttribute.AttributeName;
// }
// return null;
//}
#endregion Methods
}
}

View File

@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace JsonFx.Json
{
[AttributeUsage(AttributeTargets.All, AllowMultiple=false)]
public class JsonPropertyAttribute:JsonNameAttribute
{
public JsonPropertyAttribute():base()
{ }
public JsonPropertyAttribute(string jsonname):base(jsonname)
{
}
}
}

View File

@@ -0,0 +1,153 @@
#region License
/*---------------------------------------------------------------------------------*\
Distributed under the terms of an MIT-style license:
The MIT License
Copyright (c) 2006-2009 Stephen M. McKamey
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
\*---------------------------------------------------------------------------------*/
#endregion License
using System;
namespace JsonFx.Json
{
public class JsonSerializationException : InvalidOperationException
{
#region Init
public JsonSerializationException() : base() { }
public JsonSerializationException(string message) : base(message) { }
public JsonSerializationException(string message, Exception innerException) : base(message, innerException) { }
public JsonSerializationException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context)
: base(info, context)
{
}
#endregion Init
}
public class JsonDeserializationException : JsonSerializationException
{
#region Fields
private int index = -1;
#endregion Fields
#region Init
public JsonDeserializationException(string message, int index) : base(message)
{
this.index = index;
}
public JsonDeserializationException(string message, Exception innerException, int index)
: base(message, innerException)
{
this.index = index;
}
public JsonDeserializationException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context)
: base(info, context)
{
}
#endregion Init
#region Properties
/// <summary>
/// Gets the character position in the stream where the error occurred.
/// </summary>
public int Index
{
get { return this.index; }
}
#endregion Properties
#region Methods
/// <summary>
/// Helper method which converts the index into Line and Column numbers
/// </summary>
/// <param name="source"></param>
/// <param name="line"></param>
/// <param name="col"></param>
public void GetLineAndColumn(string source, out int line, out int col)
{
if (source == null)
{
throw new ArgumentNullException();
}
col = 1;
line = 1;
bool foundLF = false;
int i = Math.Min(this.index, source.Length);
for (; i>0; i--)
{
if (!foundLF)
{
col++;
}
if (source[i-1] == '\n')
{
line++;
foundLF = true;
}
}
}
#endregion Methods
}
public class JsonTypeCoersionException : ArgumentException
{
#region Init
public JsonTypeCoersionException() : base() { }
public JsonTypeCoersionException(string message) : base(message) { }
public JsonTypeCoersionException(string message, Exception innerException) : base(message, innerException) { }
public JsonTypeCoersionException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context)
: base(info, context)
{
}
#endregion Init
}
}

View File

@@ -0,0 +1,96 @@
#region License
/*---------------------------------------------------------------------------------*\
Distributed under the terms of an MIT-style license:
The MIT License
Copyright (c) 2006-2009 Stephen M. McKamey
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
\*---------------------------------------------------------------------------------*/
#endregion License
using System;
using System.Reflection;
namespace JsonFx.Json
{
/// <summary>
/// Specifies the name of the property which specifies if member should be serialized.
/// </summary>
[AttributeUsage(AttributeTargets.Property|AttributeTargets.Field, AllowMultiple=false)]
public class JsonSpecifiedPropertyAttribute : Attribute
{
#region Fields
private string specifiedProperty = null;
#endregion Fields
#region Init
/// <summary>
/// Ctor
/// </summary>
/// <param name="propertyName">the name of the property which controls serialization for this member</param>
public JsonSpecifiedPropertyAttribute(string propertyName)
{
this.specifiedProperty = propertyName;
}
#endregion Init
#region Properties
/// <summary>
/// Gets and sets the name of the property which
/// specifies if member should be serialized
/// </summary>
public string SpecifiedProperty
{
get { return this.specifiedProperty; }
set { this.specifiedProperty = value; }
}
#endregion Properties
#region Methods
/// <summary>
/// Gets the name specified for use in Json serialization.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string GetJsonSpecifiedProperty(MemberInfo memberInfo)
{
if (memberInfo == null ||
!Attribute.IsDefined(memberInfo, typeof(JsonSpecifiedPropertyAttribute)))
{
return null;
}
JsonSpecifiedPropertyAttribute attribute = (JsonSpecifiedPropertyAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(JsonSpecifiedPropertyAttribute));
return attribute.SpecifiedProperty;
}
#endregion Methods
}
}

View File

@@ -0,0 +1,57 @@
#region License
/*---------------------------------------------------------------------------------*\
Distributed under the terms of an MIT-style license:
The MIT License
Copyright (c) 2006-2009 Stephen M. McKamey
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
\*---------------------------------------------------------------------------------*/
#endregion License
using System;
namespace JsonFx.Json
{
/// <summary>
/// Parse Tokens
/// </summary>
internal enum JsonToken
{
End,
Undefined,
Null,
False,
True,
NaN,
PositiveInfinity,
NegativeInfinity,
Number,
String,
ArrayStart,
ArrayEnd,
ObjectStart,
ObjectEnd,
NameDelim,
ValueDelim
}
}