|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Apolyton.FastJson.Serialization;
using System.Globalization;
using System.Diagnostics;
namespace Apolyton.FastJson.Data
{
/// <summary>
/// Represents a json primitive value.
/// </summary>
public struct JsonPrimitive : IJsonValue, IConvertible
{
private readonly String localValue;
private object typedValue;
/// <summary>
/// Creates an instance of the type.
/// </summary>
/// <param name="localValue"></param>
public JsonPrimitive(String localValue)
{
this.localValue = localValue;
this.typedValue = null;
}
/// <summary>
/// Creates an instance of the type.
/// </summary>
/// <param name="localValue"></param>
public JsonPrimitive(bool boolean)
{
this.localValue = null;
this.typedValue = boolean;
}
/// <summary>
/// Gets the json primitive type.
/// </summary>
public JsonType Type
{
get { return JsonType.JsonPrimitive; }
}
/// <summary>
/// Gets the value as string.
/// </summary>
[DebuggerDisplay("Click to expand. This operation has side effects.")]
public string AsString
{
get
{
if (typedValue == null)
{
typedValue = Deserialization.ChangeType(localValue, typeof(String));
}
return (string)typedValue;
}
}
/// <summary>
/// Gets the value as integer.
/// </summary>
[DebuggerDisplay("Click to expand. This operation has side effects.")]
public int AsInteger
{
get
{
if (typedValue == null)
{
typedValue = Int32.Parse(localValue);
}
return (int)typedValue;
}
}
/// <summary>
/// Gets the value as bool.
/// </summary>
[DebuggerDisplay("Click to expand. This operation has side effects.")]
public bool AsBool
{
get
{
if (typedValue == null)
{
typedValue = bool.Parse(localValue);
}
return (bool)typedValue;
}
}
/// <summary>
/// Gets the value as double.
/// </summary>
[DebuggerDisplay("Click to expand. This operation has side effects.")]
public Double AsDouble
{
get
{
if (typedValue == null)
{
typedValue = double.Parse(localValue);
}
return (Double)typedValue;
}
}
/// <summary>
/// Gets the value as float.
/// </summary>
[DebuggerDisplay("Click to expand. This operation has side effects.")]
public float AsFloat
{
get
{
if (typedValue == null)
{
typedValue = float.Parse(localValue);
}
return (float)typedValue;
}
}
/// <summary>
/// Gets the value as date time.
/// </summary>
[DebuggerDisplay("Click to expand. This operation has side effects.")]
public DateTime AsDateTime
{
get
{
if (typedValue == null)
{
typedValue = Deserialization.CreateDateTime(localValue);
}
return (DateTime)typedValue;
}
}
/// <summary>
/// Gets the value as time span.
/// </summary>
[DebuggerDisplay("Click to expand. This operation has side effects.")]
public TimeSpan AsTimeSpan
{
get
{
if (typedValue == null)
{
typedValue = TimeSpan.Parse(localValue);
}
return (TimeSpan)typedValue;
}
}
/// <summary>
/// Gets the value as guid.
/// </summary>
[DebuggerDisplay("Click to expand. This operation has side effects.")]
public Guid AsGuid
{
get
{
if (typedValue == null)
{
typedValue = Deserialization.CreateGuid(localValue);
}
return (Guid)typedValue;
}
}
/// <summary>
/// Gets the value as long.
/// </summary>
[DebuggerDisplay("Click to expand. This operation has side effects.")]
public long AsLong
{
get
{
if (typedValue == null)
{
typedValue = Deserialization.CreateLong(localValue);
}
return (long)typedValue;
}
}
public int Count
{
get { throw new NotSupportedException(); }
}
public IJsonValue this[int i]
{
get { throw new NotSupportedException(); }
}
public IJsonValue this[string key]
{
get { throw new NotSupportedException(); }
}
#region IConvertible Members
TypeCode IConvertible.GetTypeCode()
{
return TypeCode.Boolean | TypeCode.Char | TypeCode.DateTime | TypeCode.Decimal | TypeCode.Double | TypeCode.Int16 | TypeCode.Int32 | TypeCode.Int64
| TypeCode.String | TypeCode.UInt16 | TypeCode.UInt32 | TypeCode.UInt64;
}
bool IConvertible.ToBoolean(IFormatProvider provider)
{
return AsBool;
}
byte IConvertible.ToByte(IFormatProvider provider)
{
throw new NotSupportedException();
}
char IConvertible.ToChar(IFormatProvider provider)
{
if (typedValue == null)
{
typedValue = localValue.ToCharArray().First();
}
return (char)typedValue;
}
DateTime IConvertible.ToDateTime(IFormatProvider provider)
{
return AsDateTime;
}
decimal IConvertible.ToDecimal(IFormatProvider provider)
{
return AsInteger;
}
double IConvertible.ToDouble(IFormatProvider provider)
{
return AsDouble;
}
short IConvertible.ToInt16(IFormatProvider provider)
{
return (short)AsInteger;
}
int IConvertible.ToInt32(IFormatProvider provider)
{
return AsInteger;
}
long IConvertible.ToInt64(IFormatProvider provider)
{
return AsLong;
}
sbyte IConvertible.ToSByte(IFormatProvider provider)
{
throw new NotImplementedException();
}
float IConvertible.ToSingle(IFormatProvider provider)
{
return AsFloat;
}
string IConvertible.ToString(IFormatProvider provider)
{
return localValue;
}
object IConvertible.ToType(Type conversionType, IFormatProvider provider)
{
if (conversionType == typeof(Guid))
{
return AsGuid;
}
else if (conversionType == typeof(TimeSpan))
{
return AsTimeSpan;
}
else if (conversionType.IsEnum)
{
if (typedValue != null) { return typedValue; }
else { typedValue = Deserialization.CreateEnum(conversionType, localValue); ; return typedValue; }
}
else if (conversionType == typeof(byte[]))
{
if (typedValue != null) { return typedValue; }
else { typedValue = Deserialization.CreateByteArray(localValue, typeof(byte[])); return typedValue; }
}
else if (conversionType == typeof(int?))
{
if (localValue != null) { return AsInteger; }
else { return null; }
}
else if (conversionType == typeof(double?))
{
if (localValue != null) { return AsDouble; }
else { return null; }
}
else if (conversionType == typeof(Guid?))
{
if (localValue != null) { return AsGuid; }
else { return null; }
}
else if (conversionType == typeof(short?))
{
if (localValue != null) { return (short)AsInteger; }
else { return null; }
}
else if (conversionType == typeof(long?))
{
if (localValue != null) { return AsLong; }
else { return null; }
}
else if (conversionType == typeof(DateTime?))
{
if (localValue != null) { return AsDateTime; }
else { return null; }
}
else if (conversionType == typeof(TimeSpan?))
{
if (localValue != null) { return AsTimeSpan; }
else { return null; }
}
else if (conversionType == typeof(float?))
{
if (localValue != null) { return AsFloat; }
else if (localValue == null) { return null; }
else { return null; }
}
else if (conversionType == typeof(uint?))
{
if (typedValue != null) { return typedValue; }
else if (localValue == null) { return null; }
else { typedValue = uint.Parse(localValue); return typedValue; }
}
else if (conversionType == typeof(ushort?))
{
if (typedValue != null) { return typedValue; }
else if (localValue == null) { return null; }
else { typedValue = ushort.Parse(localValue); return typedValue; }
}
else if (conversionType == typeof(ulong?))
{
if (typedValue != null) { return typedValue; }
else if (localValue == null) { return null; }
else { typedValue = ulong.Parse(localValue); return typedValue; }
}
else if (conversionType == typeof(decimal))
{
if (typedValue != null) { return typedValue; }
else { typedValue = decimal.Parse(localValue); return typedValue; }
}
else if (conversionType == typeof(decimal?))
{
if (typedValue != null) { return typedValue; }
else if (localValue == null) { return null; }
else { typedValue = decimal.Parse(localValue); return typedValue; }
}
else
{
throw new NotSupportedException("Cannot convert json primitive to: " + conversionType.ToString());
}
}
ushort IConvertible.ToUInt16(IFormatProvider provider)
{
if (typedValue == null)
{
typedValue = ushort.Parse(localValue);
}
return (ushort)typedValue;
}
uint IConvertible.ToUInt32(IFormatProvider provider)
{
if (typedValue == null)
{
typedValue = UInt32.Parse(localValue);
}
return (UInt32)typedValue;
}
ulong IConvertible.ToUInt64(IFormatProvider provider)
{
if (typedValue == null)
{
typedValue = UInt64.Parse(localValue);
}
return (UInt64)typedValue;
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.