123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516 |
- #region License and information
- /* * * * *
- *
- * Extension file for the SimpleJSON framework for better support of some common
- * .NET types. It does only work together with the SimpleJSON.cs
- * It provides direct conversion support for types like decimal, char, byte,
- * sbyte, short, ushort, uint, DateTime, TimeSpan and Guid. In addition there
- * are conversion helpers for converting an array of number values into a byte[]
- * or a List<byte> as well as converting an array of string values into a string[]
- * or List<string>.
- * Finally there are some additional type conversion operators for some nullable
- * types like short?, int?, float?, double?, long? and bool?. They will actually
- * assign a JSONNull value when it's null or a JSONNumber when it's not.
- *
- * The MIT License (MIT)
- *
- * Copyright (c) 2020 Markus Göbel (Bunny83)
- *
- * 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 and information
-
- namespace SimpleJSON
- {
- using System.Globalization;
- using System.Collections.Generic;
- public partial class JSONNode
- {
- #region Decimal
- public virtual decimal AsDecimal
- {
- get
- {
- decimal result;
- if (!decimal.TryParse(Value, out result))
- result = 0;
- return result;
- }
- set
- {
- Value = value.ToString();
- }
- }
-
- public static implicit operator JSONNode(decimal aDecimal)
- {
- return new JSONString(aDecimal.ToString());
- }
-
- public static implicit operator decimal(JSONNode aNode)
- {
- return aNode.AsDecimal;
- }
- #endregion Decimal
-
- #region Char
- public virtual char AsChar
- {
- get
- {
- if (IsString && Value.Length > 0)
- return Value[0];
- if (IsNumber)
- return (char)AsInt;
- return '\0';
- }
- set
- {
- if (IsString)
- Value = value.ToString();
- else if (IsNumber)
- AsInt = (int)value;
- }
- }
-
- public static implicit operator JSONNode(char aChar)
- {
- return new JSONString(aChar.ToString());
- }
-
- public static implicit operator char(JSONNode aNode)
- {
- return aNode.AsChar;
- }
- #endregion Decimal
-
- #region UInt
- public virtual uint AsUInt
- {
- get
- {
- return (uint)AsDouble;
- }
- set
- {
- AsDouble = value;
- }
- }
-
- public static implicit operator JSONNode(uint aUInt)
- {
- return new JSONNumber(aUInt);
- }
-
- public static implicit operator uint(JSONNode aNode)
- {
- return aNode.AsUInt;
- }
- #endregion UInt
-
- #region Byte
- public virtual byte AsByte
- {
- get
- {
- return (byte)AsInt;
- }
- set
- {
- AsInt = value;
- }
- }
-
- public static implicit operator JSONNode(byte aByte)
- {
- return new JSONNumber(aByte);
- }
-
- public static implicit operator byte(JSONNode aNode)
- {
- return aNode.AsByte;
- }
- #endregion Byte
- #region SByte
- public virtual sbyte AsSByte
- {
- get
- {
- return (sbyte)AsInt;
- }
- set
- {
- AsInt = value;
- }
- }
-
- public static implicit operator JSONNode(sbyte aSByte)
- {
- return new JSONNumber(aSByte);
- }
-
- public static implicit operator sbyte(JSONNode aNode)
- {
- return aNode.AsSByte;
- }
- #endregion SByte
-
- #region Short
- public virtual short AsShort
- {
- get
- {
- return (short)AsInt;
- }
- set
- {
- AsInt = value;
- }
- }
-
- public static implicit operator JSONNode(short aShort)
- {
- return new JSONNumber(aShort);
- }
-
- public static implicit operator short(JSONNode aNode)
- {
- return aNode.AsShort;
- }
- #endregion Short
- #region UShort
- public virtual ushort AsUShort
- {
- get
- {
- return (ushort)AsInt;
- }
- set
- {
- AsInt = value;
- }
- }
-
- public static implicit operator JSONNode(ushort aUShort)
- {
- return new JSONNumber(aUShort);
- }
-
- public static implicit operator ushort(JSONNode aNode)
- {
- return aNode.AsUShort;
- }
- #endregion UShort
-
- #region DateTime
- public virtual System.DateTime AsDateTime
- {
- get
- {
- System.DateTime result;
- if (!System.DateTime.TryParse(Value, CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
- result = new System.DateTime(0);
- return result;
- }
- set
- {
- Value = value.ToString(CultureInfo.InvariantCulture);
- }
- }
-
- public static implicit operator JSONNode(System.DateTime aDateTime)
- {
- return new JSONString(aDateTime.ToString(CultureInfo.InvariantCulture));
- }
-
- public static implicit operator System.DateTime(JSONNode aNode)
- {
- return aNode.AsDateTime;
- }
- #endregion DateTime
- #region TimeSpan
- public virtual System.TimeSpan AsTimeSpan
- {
- get
- {
- System.TimeSpan result;
- if (!System.TimeSpan.TryParse(Value, CultureInfo.InvariantCulture, out result))
- result = new System.TimeSpan(0);
- return result;
- }
- set
- {
- Value = value.ToString();
- }
- }
-
- public static implicit operator JSONNode(System.TimeSpan aTimeSpan)
- {
- return new JSONString(aTimeSpan.ToString());
- }
-
- public static implicit operator System.TimeSpan(JSONNode aNode)
- {
- return aNode.AsTimeSpan;
- }
- #endregion TimeSpan
-
- #region Guid
- public virtual System.Guid AsGuid
- {
- get
- {
- System.Guid result;
- System.Guid.TryParse(Value, out result);
- return result;
- }
- set
- {
- Value = value.ToString();
- }
- }
-
- public static implicit operator JSONNode(System.Guid aGuid)
- {
- return new JSONString(aGuid.ToString());
- }
-
- public static implicit operator System.Guid(JSONNode aNode)
- {
- return aNode.AsGuid;
- }
- #endregion Guid
-
- #region ByteArray
- public virtual byte[] AsByteArray
- {
- get
- {
- if (this.IsNull || !this.IsArray)
- return null;
- int count = Count;
- byte[] result = new byte[count];
- for (int i = 0; i < count; i++)
- result[i] = this[i].AsByte;
- return result;
- }
- set
- {
- if (!IsArray || value == null)
- return;
- Clear();
- for (int i = 0; i < value.Length; i++)
- Add(value[i]);
- }
- }
-
- public static implicit operator JSONNode(byte[] aByteArray)
- {
- return new JSONArray { AsByteArray = aByteArray };
- }
-
- public static implicit operator byte[](JSONNode aNode)
- {
- return aNode.AsByteArray;
- }
- #endregion ByteArray
- #region ByteList
- public virtual List<byte> AsByteList
- {
- get
- {
- if (this.IsNull || !this.IsArray)
- return null;
- int count = Count;
- List<byte> result = new List<byte>(count);
- for (int i = 0; i < count; i++)
- result.Add(this[i].AsByte);
- return result;
- }
- set
- {
- if (!IsArray || value == null)
- return;
- Clear();
- for (int i = 0; i < value.Count; i++)
- Add(value[i]);
- }
- }
-
- public static implicit operator JSONNode(List<byte> aByteList)
- {
- return new JSONArray { AsByteList = aByteList };
- }
-
- public static implicit operator List<byte> (JSONNode aNode)
- {
- return aNode.AsByteList;
- }
- #endregion ByteList
-
- #region StringArray
- public virtual string[] AsStringArray
- {
- get
- {
- if (this.IsNull || !this.IsArray)
- return null;
- int count = Count;
- string[] result = new string[count];
- for (int i = 0; i < count; i++)
- result[i] = this[i].Value;
- return result;
- }
- set
- {
- if (!IsArray || value == null)
- return;
- Clear();
- for (int i = 0; i < value.Length; i++)
- Add(value[i]);
- }
- }
-
- public static implicit operator JSONNode(string[] aStringArray)
- {
- return new JSONArray { AsStringArray = aStringArray };
- }
-
- public static implicit operator string[] (JSONNode aNode)
- {
- return aNode.AsStringArray;
- }
- #endregion StringArray
- #region StringList
- public virtual List<string> AsStringList
- {
- get
- {
- if (this.IsNull || !this.IsArray)
- return null;
- int count = Count;
- List<string> result = new List<string>(count);
- for (int i = 0; i < count; i++)
- result.Add(this[i].Value);
- return result;
- }
- set
- {
- if (!IsArray || value == null)
- return;
- Clear();
- for (int i = 0; i < value.Count; i++)
- Add(value[i]);
- }
- }
-
- public static implicit operator JSONNode(List<string> aStringList)
- {
- return new JSONArray { AsStringList = aStringList };
- }
-
- public static implicit operator List<string> (JSONNode aNode)
- {
- return aNode.AsStringList;
- }
- #endregion StringList
-
- #region NullableTypes
- public static implicit operator JSONNode(int? aValue)
- {
- if (aValue == null)
- return JSONNull.CreateOrGet();
- return new JSONNumber((int)aValue);
- }
- public static implicit operator int?(JSONNode aNode)
- {
- if (aNode == null || aNode.IsNull)
- return null;
- return aNode.AsInt;
- }
-
- public static implicit operator JSONNode(float? aValue)
- {
- if (aValue == null)
- return JSONNull.CreateOrGet();
- return new JSONNumber((float)aValue);
- }
- public static implicit operator float? (JSONNode aNode)
- {
- if (aNode == null || aNode.IsNull)
- return null;
- return aNode.AsFloat;
- }
-
- public static implicit operator JSONNode(double? aValue)
- {
- if (aValue == null)
- return JSONNull.CreateOrGet();
- return new JSONNumber((double)aValue);
- }
- public static implicit operator double? (JSONNode aNode)
- {
- if (aNode == null || aNode.IsNull)
- return null;
- return aNode.AsDouble;
- }
-
- public static implicit operator JSONNode(bool? aValue)
- {
- if (aValue == null)
- return JSONNull.CreateOrGet();
- return new JSONBool((bool)aValue);
- }
- public static implicit operator bool? (JSONNode aNode)
- {
- if (aNode == null || aNode.IsNull)
- return null;
- return aNode.AsBool;
- }
-
- public static implicit operator JSONNode(long? aValue)
- {
- if (aValue == null)
- return JSONNull.CreateOrGet();
- return new JSONNumber((long)aValue);
- }
- public static implicit operator long? (JSONNode aNode)
- {
- if (aNode == null || aNode.IsNull)
- return null;
- return aNode.AsLong;
- }
-
- public static implicit operator JSONNode(short? aValue)
- {
- if (aValue == null)
- return JSONNull.CreateOrGet();
- return new JSONNumber((short)aValue);
- }
- public static implicit operator short? (JSONNode aNode)
- {
- if (aNode == null || aNode.IsNull)
- return null;
- return aNode.AsShort;
- }
- #endregion NullableTypes
- }
- }
|