From 1ef39923f86c9291b99d0bab82032b9a6a08d40f Mon Sep 17 00:00:00 2001
From: alireza995 <88311706+alireza995@users.noreply.github.com>
Date: Wed, 30 Nov 2022 08:17:44 +0330
Subject: [PATCH 01/14] refactor(Solution) : Refactor all codes
Refactor all codes with:
Creating sln file
move each class in a new file
remove unusable code
create some temp changes
closes nothing
---
Folder.DotSettings | 2 +
Kavenegar.Core/Exceptions/ApiException.cs | 27 +-
Kavenegar.Core/Exceptions/BaseException.cs | 10 -
Kavenegar.Core/Exceptions/HttpException.cs | 17 -
.../Exceptions/KavenegarException.cs | 11 +
Kavenegar.Core/IKavenegarApi.cs | 277 ++++
Kavenegar.Core/IKavenegarApi2.cs | 88 ++
Kavenegar.Core/Json/JsonArray.cs | 106 +-
Kavenegar.Core/Json/JsonBoolean.cs | 31 +-
Kavenegar.Core/Json/JsonNullable.cs | 29 +-
Kavenegar.Core/Json/JsonNumber.cs | 31 +-
Kavenegar.Core/Json/JsonObject.cs | 151 +-
Kavenegar.Core/Json/JsonString.cs | 32 +-
Kavenegar.Core/Json/Parser.cs | 928 +++++------
Kavenegar.Core/Kavenegar.Core.csproj | 53 +-
Kavenegar.Core/KavenegarApi.cs | 1368 +++++++++++------
Kavenegar.Core/KavenegarApi2.cs | 430 ++++++
Kavenegar.Core/Models/AccountConfigResult.cs | 19 +-
Kavenegar.Core/Models/AccountInfoResult.cs | 22 +-
Kavenegar.Core/Models/CountInboxResult.cs | 13 +-
Kavenegar.Core/Models/CountOutboxResult.cs | 11 +-
.../Models/CountPostalCodeResult.cs | 11 +-
Kavenegar.Core/Models/Enums/MessageStatus.cs | 27 +-
Kavenegar.Core/Models/Enums/MessageType.cs | 15 +-
Kavenegar.Core/Models/Enums/MetaCode.cs | 42 +-
.../Models/Enums/VerifyLookupType.cs | 13 +-
Kavenegar.Core/Models/ReceiveResult.cs | 29 +-
Kavenegar.Core/Models/SendResult.cs | 35 +-
.../Models/StatusLocalMessageIdResult.cs | 16 +-
Kavenegar.Core/Models/StatusResult.cs | 14 +-
Kavenegar.Core/Result.cs | 7 +
Kavenegar.Core/ReturnAccountConfig.cs | 9 +
Kavenegar.Core/ReturnAccountInfo.cs | 9 +
Kavenegar.Core/ReturnCountInbox.cs | 10 +
Kavenegar.Core/ReturnCountOutbox.cs | 10 +
Kavenegar.Core/ReturnCountPostalCode.cs | 10 +
Kavenegar.Core/ReturnReceive.cs | 10 +
Kavenegar.Core/ReturnResult.cs | 7 +
Kavenegar.Core/ReturnSend.cs | 10 +
Kavenegar.Core/ReturnStatus.cs | 10 +
Kavenegar.Core/ReturnStatusLocalMessageId.cs | 10 +
Kavenegar.Core/SendMessageRequest.cs | 12 +
Kavenegar.Core/Utils/DateHelper.cs | 65 +-
Kavenegar.Core/Utils/StringHelper.cs | 18 -
Kavenegar.sln | 27 +
Kavenegar.sln.DotSettings | 2 +
.../Shared.Infrastructure/HttpClientHelper.cs | 24 +
Shared/Shared.Infrastructure/JsonUtility.cs | 27 +
.../Shared.Infrastructure.csproj | 9 +
49 files changed, 2739 insertions(+), 1405 deletions(-)
create mode 100644 Folder.DotSettings
delete mode 100644 Kavenegar.Core/Exceptions/BaseException.cs
delete mode 100644 Kavenegar.Core/Exceptions/HttpException.cs
create mode 100644 Kavenegar.Core/Exceptions/KavenegarException.cs
create mode 100644 Kavenegar.Core/IKavenegarApi.cs
create mode 100644 Kavenegar.Core/IKavenegarApi2.cs
create mode 100644 Kavenegar.Core/KavenegarApi2.cs
create mode 100644 Kavenegar.Core/Result.cs
create mode 100644 Kavenegar.Core/ReturnAccountConfig.cs
create mode 100644 Kavenegar.Core/ReturnAccountInfo.cs
create mode 100644 Kavenegar.Core/ReturnCountInbox.cs
create mode 100644 Kavenegar.Core/ReturnCountOutbox.cs
create mode 100644 Kavenegar.Core/ReturnCountPostalCode.cs
create mode 100644 Kavenegar.Core/ReturnReceive.cs
create mode 100644 Kavenegar.Core/ReturnResult.cs
create mode 100644 Kavenegar.Core/ReturnSend.cs
create mode 100644 Kavenegar.Core/ReturnStatus.cs
create mode 100644 Kavenegar.Core/ReturnStatusLocalMessageId.cs
create mode 100644 Kavenegar.Core/SendMessageRequest.cs
delete mode 100644 Kavenegar.Core/Utils/StringHelper.cs
create mode 100644 Kavenegar.sln
create mode 100644 Kavenegar.sln.DotSettings
create mode 100644 Shared/Shared.Infrastructure/HttpClientHelper.cs
create mode 100644 Shared/Shared.Infrastructure/JsonUtility.cs
create mode 100644 Shared/Shared.Infrastructure/Shared.Infrastructure.csproj
diff --git a/Folder.DotSettings b/Folder.DotSettings
new file mode 100644
index 0000000..a44ce49
--- /dev/null
+++ b/Folder.DotSettings
@@ -0,0 +1,2 @@
+
+ True
\ No newline at end of file
diff --git a/Kavenegar.Core/Exceptions/ApiException.cs b/Kavenegar.Core/Exceptions/ApiException.cs
index 5a0bbc1..4583167 100644
--- a/Kavenegar.Core/Exceptions/ApiException.cs
+++ b/Kavenegar.Core/Exceptions/ApiException.cs
@@ -1,20 +1,15 @@
using Kavenegar.Core.Models.Enums;
-namespace Kavenegar.Core.Exceptions
-{
- public class ApiException : KavenegarException
- {
- readonly MetaCode _result;
- public ApiException(string message, int code)
- : base(message)
- {
- _result = (MetaCode)code;
- }
+namespace Kavenegar.Core.Exceptions;
- public MetaCode Code
- {
- get { return _result; }
- }
+public class ApiException : KavenegarException
+{
+ public ApiException(
+ string message,
+ int code) : base(message)
+ {
+ Code = (MetaCode)code;
+ }
- }
-}
+ public MetaCode Code { get; }
+}
\ No newline at end of file
diff --git a/Kavenegar.Core/Exceptions/BaseException.cs b/Kavenegar.Core/Exceptions/BaseException.cs
deleted file mode 100644
index f30b38a..0000000
--- a/Kavenegar.Core/Exceptions/BaseException.cs
+++ /dev/null
@@ -1,10 +0,0 @@
-namespace Kavenegar.Core.Exceptions
-{
- public class KavenegarException : System.Exception
- {
- public KavenegarException(string message)
- : base(message)
- {
- }
- }
-}
\ No newline at end of file
diff --git a/Kavenegar.Core/Exceptions/HttpException.cs b/Kavenegar.Core/Exceptions/HttpException.cs
deleted file mode 100644
index e74fd19..0000000
--- a/Kavenegar.Core/Exceptions/HttpException.cs
+++ /dev/null
@@ -1,17 +0,0 @@
-namespace Kavenegar.Core.Exceptions
-{
- public class HttpException : KavenegarException
- {
- private readonly int _code;
- public HttpException(string message, int code)
- : base(message)
- {
- _code = code;
- }
-
- public int Code
- {
- get { return _code; }
- }
- }
-}
\ No newline at end of file
diff --git a/Kavenegar.Core/Exceptions/KavenegarException.cs b/Kavenegar.Core/Exceptions/KavenegarException.cs
new file mode 100644
index 0000000..49da29e
--- /dev/null
+++ b/Kavenegar.Core/Exceptions/KavenegarException.cs
@@ -0,0 +1,11 @@
+using System;
+
+namespace Kavenegar.Core.Exceptions;
+
+public class KavenegarException : Exception
+{
+ public KavenegarException(
+ string message) : base(message)
+ {
+ }
+}
\ No newline at end of file
diff --git a/Kavenegar.Core/IKavenegarApi.cs b/Kavenegar.Core/IKavenegarApi.cs
new file mode 100644
index 0000000..9ce6232
--- /dev/null
+++ b/Kavenegar.Core/IKavenegarApi.cs
@@ -0,0 +1,277 @@
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using Kavenegar.Core.Models;
+using Kavenegar.Core.Models.Enums;
+
+namespace Kavenegar.Core;
+
+public interface IKavenegarApi
+{
+ string ApiKey { set; get; }
+
+ Task> Send(
+ string sender,
+ List receptor,
+ string message);
+
+ Task Send(
+ string sender,
+ string receptor,
+ string message);
+
+ Task Send(
+ string sender,
+ string receptor,
+ string message,
+ MessageType type,
+ DateTime date);
+
+ Task> Send(
+ string sender,
+ List receptor,
+ string message,
+ MessageType type,
+ DateTime date);
+
+ Task Send(
+ string sender,
+ string receptor,
+ string message,
+ MessageType type,
+ DateTime date,
+ string localId);
+
+ Task Send(
+ string sender,
+ string receptor,
+ string message,
+ string localId);
+
+ Task> Send(
+ string sender,
+ List receptors,
+ string message,
+ string localId);
+
+ Task> Send(
+ string sender,
+ List receptor,
+ string message,
+ MessageType type,
+ DateTime date,
+ List localIds);
+
+ Task> SendArray(
+ List senders,
+ List receptors,
+ List messages);
+
+ Task> SendArray(
+ string sender,
+ List receptors,
+ List messages,
+ MessageType type,
+ DateTime date);
+
+ Task> SendArray(
+ string sender,
+ List receptors,
+ List messages,
+ MessageType type,
+ DateTime date,
+ string localMessageIds);
+
+ Task> SendArray(
+ string sender,
+ List receptors,
+ List messages,
+ string localMessageId);
+
+ Task> SendArray(
+ List senders,
+ List receptors,
+ List messages,
+ string localMessageId);
+
+ Task> SendArray(
+ List senders,
+ List receptors,
+ List messages,
+ List types,
+ DateTime date,
+ List localMessageIds);
+
+ Task> Status(
+ List messageIds);
+
+ Task Status(
+ string messageId);
+
+ Task> StatusLocalMessageId(
+ List messageIds);
+
+ Task StatusLocalMessageId(
+ string messageId);
+
+ Task> Select(
+ List messageIds);
+
+ Task Select(
+ string messageId);
+
+ Task> SelectOutbox(
+ DateTime startDate);
+
+ Task> SelectOutbox(
+ DateTime startDate,
+ DateTime endDate);
+
+ Task> SelectOutbox(
+ DateTime startDate,
+ DateTime endDate,
+ string sender);
+
+ Task> LatestOutbox(
+ long pageSize);
+
+ Task> LatestOutbox(
+ long pageSize,
+ string sender);
+
+ Task CountOutbox(
+ DateTime startDate);
+
+ Task CountOutbox(
+ DateTime startDate,
+ DateTime endDate);
+
+ Task CountOutbox(
+ DateTime startDate,
+ DateTime endDate,
+ int status);
+
+ Task> Cancel(
+ List ids);
+
+ Task Cancel(
+ string messageId);
+
+ Task> Receive(
+ string line,
+ int isRead);
+
+ Task CountInbox(
+ DateTime startDate,
+ string lineNumber);
+
+ Task CountInbox(
+ DateTime startDate,
+ DateTime endDate,
+ string lineNumber);
+
+ Task CountInbox(
+ DateTime startDate,
+ DateTime endDate,
+ string lineNumber,
+ int isRead);
+
+ Task> CountPostalCode(
+ long postalcode);
+
+ Task> SendByPostalCode(
+ long postalcode,
+ string sender,
+ string message,
+ long mciStartIndex,
+ long mciCount,
+ long mtnStartIndex,
+ long mtnCount);
+
+ Task> SendByPostalCode(
+ long postalcode,
+ string sender,
+ string message,
+ long mciStartIndex,
+ long mciCount,
+ long mtnStartIndex,
+ long mtnCount,
+ DateTime date);
+
+ Task AccountInfo();
+
+ Task AccountConfig(
+ string apiLogs,
+ string dailyReport,
+ string debugMode,
+ string defaultSender,
+ int? minCreditAlarm,
+ string resendFailed);
+
+ Task VerifyLookup(
+ string receptor,
+ string token,
+ string template);
+
+ Task VerifyLookup(
+ string receptor,
+ string token,
+ string template,
+ VerifyLookupType type);
+
+ Task VerifyLookup(
+ string receptor,
+ string token,
+ string token2,
+ string token3,
+ string template);
+
+ Task VerifyLookup(
+ string receptor,
+ string token,
+ string token2,
+ string token3,
+ string token10,
+ string template);
+
+ Task VerifyLookup(
+ string receptor,
+ string token,
+ string token2,
+ string token3,
+ string template,
+ VerifyLookupType type);
+
+ Task VerifyLookup(
+ string receptor,
+ string token,
+ string token2,
+ string token3,
+ string token10,
+ string template,
+ VerifyLookupType type);
+
+ Task VerifyLookup(
+ string receptor,
+ string token,
+ string token2,
+ string token3,
+ string token10,
+ string token20,
+ string template,
+ VerifyLookupType type);
+
+ Task CallMakeTts(
+ string message,
+ string receptor);
+
+ Task> CallMakeTts(
+ string message,
+ List receptor);
+
+ Task> CallMakeTts(
+ string message,
+ List receptor,
+ DateTime? date,
+ List localId);
+}
\ No newline at end of file
diff --git a/Kavenegar.Core/IKavenegarApi2.cs b/Kavenegar.Core/IKavenegarApi2.cs
new file mode 100644
index 0000000..ab9d370
--- /dev/null
+++ b/Kavenegar.Core/IKavenegarApi2.cs
@@ -0,0 +1,88 @@
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using Kavenegar.Core.Models;
+using Kavenegar.Core.Models.Enums;
+
+namespace Kavenegar.Core;
+
+public interface IKavenegarApi2
+{
+ string ApiKey { set; get; }
+
+ Task> Send(
+ SendMessageRequest message,
+ DateTime? date);
+
+ Task> SendArray(
+ List messages,
+ DateTime? date);
+
+ Task Status(
+ string messageId);
+
+ Task> Status(
+ List messageIds);
+
+ Task StatusLocalMessageId(
+ string messageId);
+
+ Task> StatusLocalMessageId(
+ List messageIds);
+
+ Task Select(
+ string messageId);
+
+ Task> Select(
+ List messageIds);
+
+ Task> SelectOutbox(
+ DateTime startDate,
+ DateTime? endDate,
+ string? sender);
+
+ Task> LatestOutbox(
+ long? pageSize,
+ string? sender);
+
+ Task CountOutbox(
+ DateTime startDate,
+ DateTime? endDate,
+ int? status);
+
+ Task Cancel(
+ string messageId);
+
+ Task> Cancel(
+ List ids);
+
+ Task> Receive(
+ string line,
+ int isRead);
+
+ Task CountInbox(
+ DateTime startDate,
+ DateTime? endDate,
+ string? lineNumber,
+ int? isRead);
+
+ Task AccountInfo();
+
+ Task AccountConfig(
+ string apiLogs,
+ string dailyReport,
+ string debugMode,
+ string defaultSender,
+ int? minCreditAlarm,
+ string resendFailed);
+
+ Task VerifyLookup(
+ string receptor,
+ string template,
+ string token1,
+ string? token2,
+ string? token3,
+ string? token4,
+ string? token5,
+ VerifyLookupType? type);
+}
\ No newline at end of file
diff --git a/Kavenegar.Core/Json/JsonArray.cs b/Kavenegar.Core/Json/JsonArray.cs
index ce2d110..f0b88ff 100644
--- a/Kavenegar.Core/Json/JsonArray.cs
+++ b/Kavenegar.Core/Json/JsonArray.cs
@@ -1,57 +1,53 @@
-using System;
-using System.Collections.Generic;
+using System.Collections.Generic;
-namespace Kavenegar.Core.Json
-{
+namespace Kavenegar.Core.Json;
- public class JsonArray : JsonObject
- {
- public List Array { get; set; }
- private List.Enumerator _e;
-
- public JsonArray()
- {
- Array = new List();
- }
-
- public void AddElementToArray(JsonObject arrayElement)
- {
- Array.Add(arrayElement);
- }
-
- public JsonObject UpCast()
- {
- JsonObject objectJ = this;
- return objectJ;
- }
-
- public void AddList(List lista)
- {
- Array = lista;
- }
-
- public Boolean NextObject(out JsonObject o)
- {
-
- JsonObject outObject;
- _e = Array.GetEnumerator();
-
- if (_e.MoveNext())
- {
- outObject = _e.Current;
- o = outObject;
- return true;
- }
- outObject = new JsonObject();
- o = outObject;
- return false;
- }
-
- public int Count
- {
- get { return Array.Count; }
- }
-
- }
-
-}
+public class JsonArray : JsonObject
+{
+ private List.Enumerator _e;
+
+ public JsonArray()
+ {
+ Array = new List();
+ }
+
+ public List Array { get; set; }
+
+ public int Count => Array.Count;
+
+ public void AddElementToArray(
+ JsonObject arrayElement)
+ {
+ Array.Add(arrayElement);
+ }
+
+ public JsonObject UpCast()
+ {
+ JsonObject objectJ = this;
+ return objectJ;
+ }
+
+ public void AddList(
+ List lista)
+ {
+ Array = lista;
+ }
+
+ public bool NextObject(
+ out JsonObject o)
+ {
+ JsonObject outObject;
+ _e = Array.GetEnumerator();
+
+ if (_e.MoveNext())
+ {
+ outObject = _e.Current;
+ o = outObject;
+ return true;
+ }
+
+ outObject = new JsonObject();
+ o = outObject;
+ return false;
+ }
+}
\ No newline at end of file
diff --git a/Kavenegar.Core/Json/JsonBoolean.cs b/Kavenegar.Core/Json/JsonBoolean.cs
index 3708b6f..d944ef2 100644
--- a/Kavenegar.Core/Json/JsonBoolean.cs
+++ b/Kavenegar.Core/Json/JsonBoolean.cs
@@ -1,21 +1,18 @@
-using System;
+namespace Kavenegar.Core.Json;
-namespace Kavenegar.Core.Json
+public class JsonBoolean : JsonObject
{
+ public JsonBoolean(
+ bool booleanValue)
+ {
+ BooleanValue = booleanValue;
+ }
- public class JsonBoolean : JsonObject
- {
- public Boolean BooleanValue { get; set; }
+ public bool BooleanValue { get; set; }
- public JsonBoolean(Boolean booleanValue)
- {
- BooleanValue = booleanValue;
- }
-
- public JsonObject UpCast()
- {
- JsonObject objectJ = this;
- return objectJ;
- }
- }
-}
+ public JsonObject UpCast()
+ {
+ JsonObject objectJ = this;
+ return objectJ;
+ }
+}
\ No newline at end of file
diff --git a/Kavenegar.Core/Json/JsonNullable.cs b/Kavenegar.Core/Json/JsonNullable.cs
index f3a7b2f..6d198d4 100644
--- a/Kavenegar.Core/Json/JsonNullable.cs
+++ b/Kavenegar.Core/Json/JsonNullable.cs
@@ -1,20 +1,17 @@
-using System;
+namespace Kavenegar.Core.Json;
-namespace Kavenegar.Core.Json
+public class JsonNullable : JsonObject
{
- public class JsonNullable : JsonObject
- {
- public String Nullable { get; set; }
+ public JsonNullable()
+ {
+ Nullable = "Null";
+ }
- public JsonNullable()
- {
- Nullable = "Null";
- }
+ public string Nullable { get; set; }
- public JsonObject UpCast()
- {
- JsonObject objectJ = this;
- return objectJ;
- }
- }
-}
+ public JsonObject UpCast()
+ {
+ JsonObject objectJ = this;
+ return objectJ;
+ }
+}
\ No newline at end of file
diff --git a/Kavenegar.Core/Json/JsonNumber.cs b/Kavenegar.Core/Json/JsonNumber.cs
index 69b0de6..f4dee4e 100644
--- a/Kavenegar.Core/Json/JsonNumber.cs
+++ b/Kavenegar.Core/Json/JsonNumber.cs
@@ -1,21 +1,18 @@
-using Kavenegar.Core.Json;
+namespace Kavenegar.Core.Json;
-namespace Kavenegar.Core.Json
+public class JsonNumber : JsonObject
{
- public class JsonNumber : JsonObject
- {
- public float Number { get; set; }
+ public JsonNumber(
+ float number)
+ {
+ Number = number;
+ }
- public JsonNumber(float number)
- {
- Number = number;
- }
+ public float Number { get; set; }
- public JsonObject UpCast()
- {
- JsonObject objectJ = this;
- return objectJ;
- }
- }
-
-}
+ public JsonObject UpCast()
+ {
+ JsonObject objectJ = this;
+ return objectJ;
+ }
+}
\ No newline at end of file
diff --git a/Kavenegar.Core/Json/JsonObject.cs b/Kavenegar.Core/Json/JsonObject.cs
index 0b79998..224df99 100644
--- a/Kavenegar.Core/Json/JsonObject.cs
+++ b/Kavenegar.Core/Json/JsonObject.cs
@@ -1,100 +1,83 @@
-using System;
using System.Collections.Generic;
-namespace Kavenegar.Core.Json
+namespace Kavenegar.Core.Json;
+
+///
+/// JsonObject is the base class.
+/// JsonString,JsonNumber,JsonBoolean,JsonNullable and JsonArray inherits from JsonObject.
+/// A JsonArray object may contain objects of the base class
+///
+public class JsonObject
{
- ///
- /// JsonObject is the base class.
- /// JsonString,JsonNumber,JsonBoolean,JsonNullable and JsonArray inherits from JsonObject.
- /// A JsonArray object may contain objects of the base class
- ///
+ public Dictionary Values;
- public class JsonObject
- {
- public Dictionary Values;
+ public JsonObject()
+ {
+ Values = new Dictionary();
+ }
- public JsonObject()
- {
- Values = new Dictionary();
- }
+ public void AddJsonValue(
+ string textTag,
+ JsonObject newObject)
+ {
+ if (!Values.ContainsKey(textTag)) Values.Add(textTag, newObject);
+ }
- public void AddJsonValue(String textTag, JsonObject newObject)
- {
- if (!Values.ContainsKey(textTag))
- {
- Values.Add(textTag, newObject);
- }
- }
+ public JsonObject GetObject(
+ string key)
+ {
+ var current = Values[key];
+ return current;
+ }
- public JsonObject GetObject(String key)
- {
- JsonObject current = Values[key];
- return current;
- }
+ public int ElementsOfDictionary()
+ {
+ return Values.Count;
+ }
- public int ElementsOfDictionary()
- {
- return Values.Count;
- }
+ public bool IsJsonString()
+ {
+ if (this is JsonString) return true;
+ return false;
+ }
- public Boolean IsJsonString()
- {
- if (this is JsonString)
- {
- return true;
- }
- return false;
- }
+ public bool IsJsonNumber()
+ {
+ if (this is JsonNumber) return true;
+ return false;
+ }
- public Boolean IsJsonNumber()
- {
- if (this is JsonNumber)
- {
- return true;
- }
- return false;
- }
+ public bool IsJsonBoolean()
+ {
+ if (this is JsonBoolean) return true;
+ return false;
+ }
- public Boolean IsJsonBoolean()
- {
- if (this is JsonBoolean)
- {
- return true;
- }
- return false;
- }
+ public bool IsJsonNullable()
+ {
+ if (this is JsonNullable) return true;
+ return false;
+ }
- public Boolean IsJsonNullable()
- {
- if (this is JsonNullable)
- {
- return true;
- }
- return false;
- }
+ public bool IsJsonArray()
+ {
+ if (this is JsonArray) return true;
+ return false;
+ }
- public Boolean IsJsonArray()
- {
- if (this is JsonArray)
- {
- return true;
- }
- return false;
- }
+ public JsonString GetAsString()
+ {
+ return (JsonString)this;
+ }
- public JsonString GetAsString()
- {
- return (JsonString)this;
- }
- public JsonNumber GetAsNumber()
- {
- return (JsonNumber)this;
- }
- public JsonArray GetAsArray()
- {
- return (JsonArray)this;
- }
- }
-}
+ public JsonNumber GetAsNumber()
+ {
+ return (JsonNumber)this;
+ }
+ public JsonArray GetAsArray()
+ {
+ return (JsonArray)this;
+ }
+}
\ No newline at end of file
diff --git a/Kavenegar.Core/Json/JsonString.cs b/Kavenegar.Core/Json/JsonString.cs
index b2031d2..cdbb011 100644
--- a/Kavenegar.Core/Json/JsonString.cs
+++ b/Kavenegar.Core/Json/JsonString.cs
@@ -1,22 +1,18 @@
-using System;
+namespace Kavenegar.Core.Json;
-namespace Kavenegar.Core.Json
+public class JsonString : JsonObject
{
- public class JsonString : JsonObject
- {
- public String Text { get; set; }
+ public JsonString(
+ string text)
+ {
+ Text = text;
+ }
- public JsonString(String text)
- {
- Text = text;
- }
+ public string Text { get; set; }
- public JsonObject UpCast()
- {
- JsonObject objectJ = this;
- return objectJ;
- }
-
-
- }
-}
+ public JsonObject UpCast()
+ {
+ JsonObject objectJ = this;
+ return objectJ;
+ }
+}
\ No newline at end of file
diff --git a/Kavenegar.Core/Json/Parser.cs b/Kavenegar.Core/Json/Parser.cs
index cb61562..604b6f7 100644
--- a/Kavenegar.Core/Json/Parser.cs
+++ b/Kavenegar.Core/Json/Parser.cs
@@ -1,455 +1,481 @@
-// SharpYourJson
-// (c) 2013 Felipe Herranz
-// SharpYourJson may be freely distributed under the MIT license.
-
using System;
using System.Collections.Generic;
-namespace Kavenegar.Core.Json
-{
- ///
- /// Contains the façade-style class to perform Json operations
- ///
-
- public class Parser
- {
-
- public JsonObject DocumentJson; // The deserialized Json Object will be stored here
-
- private const char ObjectBegin = '{';
- private const char ObjectEnd = '}';
- private const char ArrayBegin = '[';
- private const char ArrayEnd = ']';
- private const char DoubleQuotes = '"';
- private const char DoublePoint = ':';
- private const char Comma = ',';
- private const char BackSlash = '\u005C';
- private const string NullValue = "null";
- private const string TrueValue = "true";
- private const string FalseValue = "false";
-
- ///
- /// Deserialize a JSON document. This method does not perform a syntax checking so It assumes a valid Json input
- ///
- ///
- ///
- /// A string which contains a valid Json array or object
- ///
- public JsonObject Parse(String json)
- {
- if (json[0] == ArrayBegin)
- {
- json = json.Substring(1, json.Length - 2);
- JsonArray arrayJson = SerializeArray(json);
- JsonObject o = arrayJson;
- return o;
-
- }
- else if (json[0] == ObjectBegin)
- {
-
- return SerializeObject(json);
- }
- return null;
- }
- ///
- /// This method performs deserialization of an object(except array)
- ///
- ///
- /// JsonObject object as a deserialized JSON object
- ///
- ///
- /// A string which contains a valid Json object
- ///
- private JsonObject SerializeObject(String json)
- {
- json = json.Replace(@"\", "");
- JsonObject document = new JsonObject();
- int n = 1;
- int lengthJson = json.Length;
- String keyString = "";
-
- while (n <= lengthJson - 1)
- {
- if (json[n] == DoubleQuotes && json[n - 1] != DoublePoint) // (key-value Pair) key Name
- {
- int secondDoubleQuotes = FindNextQuote(json, n + 1);
- keyString = json.Substring(n + 1, (secondDoubleQuotes - (n + 1)));
- n = secondDoubleQuotes + 1;
-
- }
- else if (json[n] == DoubleQuotes && json[n - 1] == DoublePoint) // (key-value Pair) value Name (if string)
- {
- if (json[n + 1] != DoubleQuotes)
- {
- int secondDoublesQuotes = FindNextQuote(json, n + 1);
- String text = json.Substring(n + 1, (secondDoublesQuotes - (n + 1)));
- JsonString stringValue = new JsonString(text);
- JsonObject o = stringValue;
- document.AddJsonValue(keyString, o);
- n = secondDoublesQuotes + 1;
- }
- else
- {
- JsonObject o = new JsonString("");
- document.AddJsonValue(keyString, o);
- }
-
- }
- else if (json[n] == '-' || json[n] == '0' || json[n] == '1' || json[n] == '2' || json[n] == '3' ||
- json[n] == '4' || json[n] == '5' || json[n] == '6' || json[n] == '7' || json[n] == '8' ||
- json[n] == '9') // (key-value Pair) value (if number)
- {
- char[] arrayEndings = { ObjectEnd, Comma };
- int nextComma = json.IndexOfAny(arrayEndings, n);
- String stringNumber = json.Substring(n, nextComma - n);
- Double valueNumber = Convert.ToDouble(stringNumber);
- float floatNumber = (float)valueNumber;
- JsonNumber number = new JsonNumber(floatNumber);
- JsonObject o = number;
- document.AddJsonValue(keyString, o);
- n = nextComma + 1;
-
- }
- else if (json[n] == ArrayBegin) //(key-value Pair) value (if array)
- {
- if (json[n + 1] != ArrayEnd)
- {
- String subJson = json.Substring(n, json.Length - n);
- int arrayClose = CloseBracketArray(subJson);
- String arrayUnknown = json.Substring(n + 1, arrayClose - 2);
- JsonArray arrayObjects = SerializeArray(arrayUnknown);
- JsonObject o = arrayObjects;
- document.AddJsonValue(keyString, o);
- n = n + arrayClose;
- }
- else
- {
- if (!string.IsNullOrEmpty(keyString))
- {
- JsonArray arrayTempEmpty = new JsonArray { Array = null };
- JsonObject emptyArray = arrayTempEmpty;
- document.AddJsonValue(keyString, emptyArray);
- keyString = "";
-
- }
- else
- {
- n++;
- }
- }
-
- }
- else if (json[n] == ObjectBegin) // (key-value Pair) value (if object)
- {
- if (json[n + 1] != ObjectEnd)
- {
- String subJson = json.Substring(n, json.Length - n);
- int objectClose = CloseBracketObject(subJson);
- String objectUnknown = json.Substring(n, objectClose);
- var o = SerializeObject(objectUnknown);
- document.AddJsonValue(keyString, o);
- n = n + objectClose + 1;
- }
- else
- {
- JsonObject o = new JsonObject { Values = null };
- document.AddJsonValue(keyString, o);
- }
-
- }
- else if (String.Compare(SafeSubString(json, n, 4), NullValue, StringComparison.Ordinal) == 0) // (key-value Pair) value (if NULL)
- {
- JsonObject o = new JsonNullable();
- document.AddJsonValue(keyString, o);
- n = n + 5;
- }
- else if (String.Compare(SafeSubString(json, n, 4), TrueValue, StringComparison.Ordinal) == 0) // (key-value Pair) value (if TRUE)
- {
- JsonObject o = new JsonBoolean(true);
- document.AddJsonValue(keyString, o);
- n = n + 5;
- }
- else if (String.Compare(SafeSubString(json, n, 5), FalseValue, StringComparison.Ordinal) == 0) // (key-value Pair) value (if FALSE)
- {
- JsonObject o = new JsonBoolean(false);
- document.AddJsonValue(keyString, o);
- n = n + 6;
- }
- else
- {
- n++;
- }
-
- }
-
- return document;
- }
-
- ///
- /// Search where is the ending of an object
- ///
- ///
- /// the index of the '}' which closes an object
- ///
- ///
- /// A valid json string ({........)
- ///
-
- private int CloseBracketObject(String json)
- {
- int countObjectBegin = 0;
- int countObjectEnd = 0;
- int n = 0;
-
- do
- {
- if (json[n] == ObjectBegin)
- {
- countObjectBegin++;
-
- }
- else if (json[n] == ObjectEnd)
- {
- countObjectEnd++;
- }
-
- n++;
-
- } while (countObjectBegin != countObjectEnd);
-
- return n;
- }
-
- ///
- /// Search where is the ending of an array
- ///
- ///
- /// he index of the ']' which closes an object
- ///
- ///
- /// A valid Json string ([.....)
- ///
-
- private int CloseBracketArray(String json)
- {
- int countArrayBegin = 0;
- int countArrayEnd = 0;
- int n = 0;
-
- do
- {
- if (json[n] == ArrayBegin)
- {
- countArrayBegin++;
-
- }
- else if (json[n] == ArrayEnd)
- {
- countArrayEnd++;
- }
-
- n++;
-
- } while (countArrayBegin != countArrayEnd);
-
- return n;
- }
-
- ///
- /// Deserialize a Json Array into an object JsonArray
- ///
- ///
- /// JsonArray object as a deserialized JSON array
- ///
- ///
- /// valid JSON array except the brackets
- ///
-
- private JsonArray SerializeArray(String array)
- {
- JsonArray arrayObject = new JsonArray();
- var elements = SplitElements(array);
-
- foreach (String item in elements)
- {
-
- if (item[0] == DoubleQuotes)
- {
- String withoutQuotes = item.Trim(DoubleQuotes);
- JsonObject o = new JsonString(withoutQuotes);
- arrayObject.AddElementToArray(o);
-
- }
- else if (item[0] == ObjectBegin)
- {
- JsonObject o = SerializeObject(item);
- arrayObject.AddElementToArray(o);
-
- }
- else if (item[0] == ArrayBegin)
- {
- String itemArray = item.Substring(1, item.Length - 2);
- JsonArray secondaryArray = SerializeArray(itemArray);
- JsonObject o = secondaryArray;
- arrayObject.AddElementToArray(o);
-
- }
- else if (item[0] == '-' || item[0] == '0' || item[0] == '1' || item[0] == '2' || item[0] == '3' ||
- item[0] == '4' || item[0] == '5' || item[0] == '6' || item[0] == '7' || item[0] == '8' || item[0] == '9')
- {
- Double doubleValue = Convert.ToDouble(item);
- float floatValue = (float)doubleValue;
- JsonObject o = new JsonNumber(floatValue);
- arrayObject.AddElementToArray(o);
- }
- else if (String.Compare(SafeSubString(item, 0, 4), TrueValue, StringComparison.Ordinal) == 0)
- {
- JsonObject o = new JsonBoolean(true);
- arrayObject.AddElementToArray(o);
- }
- else if (String.Compare(SafeSubString(item, 0, 5), FalseValue, StringComparison.Ordinal) == 0)
- {
- JsonObject o = new JsonBoolean(false);
- arrayObject.AddElementToArray(o);
- }
- else if (String.Compare(SafeSubString(item, 0, 4), NullValue, StringComparison.Ordinal) == 0)
- {
- JsonObject o = new JsonNullable();
- arrayObject.AddElementToArray(o);
- }
-
- }
-
- return arrayObject;
-
- }
- ///
- /// Just a safe subString operation
- ///
- ///
- /// A subString of the string input parameter text
- ///
- ///
- /// A string
- ///
- ///
- /// index of starting
- ///
- ///
- /// Length of the subString
- ///
-
- private String SafeSubString(String text, int start, int length)
- {
- var safeString = start + length < text.Length ? text.Substring(start, length) : text.Substring(start, text.Length - start);
-
- return safeString;
- }
-
- ///
- /// Finds the next '"' to close a String field
- ///
- ///
- /// The next ' " '
- ///
- ///
- /// A valid JSON string
- ///
- ///
- /// Index of starting
- ///
-
- private int FindNextQuote(String text, int index)
- {
- int nextQuote = text.IndexOf(DoubleQuotes, index);
- while (text[nextQuote - 1] == BackSlash)
- {
- nextQuote = text.IndexOf(DoubleQuotes, nextQuote + 1);
- }
-
- return nextQuote;
-
- }
-
- ///
- /// Splits the elements of an Array
- ///
- ///
- /// The elements in an array of Strings
- ///
- ///
- ///
- ///
-
- private String[] SplitElements(String arrayText)
- {
- int n = 0;
- int doubleQuotesCounter = 0;
- int objectBeginCounter = 0;
- int objectEndCounter = 0;
- int arrayBeginCounter = 0;
- int arrayEndCounter = 0;
- int previousCommaIndex = 0;
- Boolean oneElement = true;
- List textSplit = new List();
-
- while (n <= arrayText.Length - 1)
- {
- if (arrayText[n] == DoubleQuotes && arrayText[n - 1] != BackSlash)
- {
- doubleQuotesCounter++;
- n++;
- }
- else if (arrayText[n] == ObjectBegin)
- {
- objectBeginCounter++;
- n++;
-
- }
- else if (arrayText[n] == ObjectEnd)
- {
- objectEndCounter++;
- n++;
-
- }
- else if (arrayText[n] == ArrayBegin)
- {
- arrayBeginCounter++;
- n++;
-
- }
- else if (arrayText[n] == ArrayEnd)
- {
- arrayEndCounter++;
- n++;
-
- }
- else if (arrayText[n] == Comma && doubleQuotesCounter % 2 == 0 && objectBeginCounter == objectEndCounter
- && arrayBeginCounter == arrayEndCounter)
- {
- textSplit.Add(arrayText.Substring(previousCommaIndex, (n - previousCommaIndex)));
- previousCommaIndex = n + 1;
- n++;
- oneElement = false;
-
- }
- else
- {
- n++;
- }
- }
-
- textSplit.Add(oneElement
- ? arrayText
- : arrayText.Substring(previousCommaIndex, (arrayText.Length) - previousCommaIndex));
-
- String[] textSplitArray = textSplit.ToArray();
- return textSplitArray;
- }
-
- }
-}
-
-
+namespace Kavenegar.Core.Json;
+///
+/// Contains the façade-style class to perform Json operations
+///
+public class Parser
+{
+ private const char ObjectBegin = '{';
+ private const char ObjectEnd = '}';
+ private const char ArrayBegin = '[';
+ private const char ArrayEnd = ']';
+ private const char DoubleQuotes = '"';
+ private const char DoublePoint = ':';
+ private const char Comma = ',';
+ private const char BackSlash = '\u005C';
+ private const string NullValue = "null";
+ private const string TrueValue = "true";
+ private const string FalseValue = "false";
+
+ public JsonObject DocumentJson;
+
+ ///
+ /// Deserialize a JSON document. This method does not perform a syntax checking so It assumes a valid Json input
+ ///
+ ///
+ /// A string which contains a valid Json array or object
+ ///
+ public JsonObject Parse(
+ string json)
+ {
+ if (json[0] == ArrayBegin)
+ {
+ json = json.Substring(1, json.Length - 2);
+ var arrayJson = SerializeArray(json);
+ JsonObject o = arrayJson;
+ return o;
+ }
+
+ if (json[0] == ObjectBegin) return SerializeObject(json);
+
+ return null;
+ }
+
+ ///
+ /// This method performs deserialization of an object(except array)
+ ///
+ ///
+ /// JsonObject object as a deserialized JSON object
+ ///
+ ///
+ /// A string which contains a valid Json object
+ ///
+ private JsonObject SerializeObject(
+ string json)
+ {
+ json = json.Replace(@"\", "");
+ var document = new JsonObject();
+ var n = 1;
+ var lengthJson = json.Length;
+ var keyString = "";
+
+ while (n <= lengthJson - 1)
+ if (json[n] == DoubleQuotes &&
+ json[n - 1] != DoublePoint)
+ {
+ var secondDoubleQuotes = FindNextQuote(json, n + 1);
+ keyString = json.Substring(n + 1, secondDoubleQuotes - (n + 1));
+ n = secondDoubleQuotes + 1;
+ }
+ else if (json[n] == DoubleQuotes &&
+ json[n - 1] == DoublePoint)
+ {
+ if (json[n + 1] != DoubleQuotes)
+ {
+ var secondDoublesQuotes = FindNextQuote(json, n + 1);
+ var text = json.Substring(n + 1, secondDoublesQuotes - (n + 1));
+ var stringValue = new JsonString(text);
+ JsonObject o = stringValue;
+ document.AddJsonValue(keyString, o);
+ n = secondDoublesQuotes + 1;
+ }
+ else
+ {
+ JsonObject o = new JsonString("");
+ document.AddJsonValue(keyString, o);
+ }
+ }
+ else if (json[n] == '-' ||
+ json[n] == '0' ||
+ json[n] == '1' ||
+ json[n] == '2' ||
+ json[n] == '3' ||
+ json[n] == '4' ||
+ json[n] == '5' ||
+ json[n] == '6' ||
+ json[n] == '7' ||
+ json[n] == '8' ||
+ json[n] == '9')
+ {
+ char[] arrayEndings =
+ {
+ ObjectEnd,
+ Comma
+ };
+ var nextComma = json.IndexOfAny(arrayEndings, n);
+ var stringNumber = json.Substring(n, nextComma - n);
+ var valueNumber = Convert.ToDouble(stringNumber);
+ var floatNumber = (float)valueNumber;
+ var number = new JsonNumber(floatNumber);
+ JsonObject o = number;
+ document.AddJsonValue(keyString, o);
+ n = nextComma + 1;
+ }
+ else if (json[n] == ArrayBegin)
+ {
+ if (json[n + 1] != ArrayEnd)
+ {
+ var subJson = json.Substring(n, json.Length - n);
+ var arrayClose = CloseBracketArray(subJson);
+ var arrayUnknown = json.Substring(n + 1, arrayClose - 2);
+ var arrayObjects = SerializeArray(arrayUnknown);
+ JsonObject o = arrayObjects;
+ document.AddJsonValue(keyString, o);
+ n = n + arrayClose;
+ }
+ else
+ {
+ if (!string.IsNullOrEmpty(keyString))
+ {
+ var arrayTempEmpty = new JsonArray
+ {
+ Array = null
+ };
+ JsonObject emptyArray = arrayTempEmpty;
+ document.AddJsonValue(keyString, emptyArray);
+ keyString = "";
+ }
+ else
+ {
+ n++;
+ }
+ }
+ }
+ else if (json[n] == ObjectBegin)
+ {
+ if (json[n + 1] != ObjectEnd)
+ {
+ var subJson = json.Substring(n, json.Length - n);
+ var objectClose = CloseBracketObject(subJson);
+ var objectUnknown = json.Substring(n, objectClose);
+ var o = SerializeObject(objectUnknown);
+ document.AddJsonValue(keyString, o);
+ n = n + objectClose + 1;
+ }
+ else
+ {
+ var o = new JsonObject
+ {
+ Values = null
+ };
+ document.AddJsonValue(keyString, o);
+ }
+ }
+ else if (string.Compare(
+ SafeSubString(
+ json,
+ n,
+ 4),
+ NullValue,
+ StringComparison.Ordinal) ==
+ 0)
+ {
+ JsonObject o = new JsonNullable();
+ document.AddJsonValue(keyString, o);
+ n = n + 5;
+ }
+ else if (string.Compare(
+ SafeSubString(
+ json,
+ n,
+ 4),
+ TrueValue,
+ StringComparison.Ordinal) ==
+ 0)
+ {
+ JsonObject o = new JsonBoolean(true);
+ document.AddJsonValue(keyString, o);
+ n = n + 5;
+ }
+ else if (string.Compare(
+ SafeSubString(
+ json,
+ n,
+ 5),
+ FalseValue,
+ StringComparison.Ordinal) ==
+ 0)
+ {
+ JsonObject o = new JsonBoolean(false);
+ document.AddJsonValue(keyString, o);
+ n = n + 6;
+ }
+ else
+ {
+ n++;
+ }
+
+ return document;
+ }
+
+ ///
+ /// Search where is the ending of an object
+ ///
+ ///
+ /// the index of the '}' which closes an object
+ ///
+ ///
+ /// A valid json string ({........)
+ ///
+ private int CloseBracketObject(
+ string json)
+ {
+ var countObjectBegin = 0;
+ var countObjectEnd = 0;
+ var n = 0;
+
+ do
+ {
+ if (json[n] == ObjectBegin)
+ countObjectBegin++;
+ else if (json[n] == ObjectEnd) countObjectEnd++;
+
+ n++;
+ }
+ while (countObjectBegin != countObjectEnd);
+
+ return n;
+ }
+
+ ///
+ /// Search where is the ending of an array
+ ///
+ ///
+ /// he index of the ']' which closes an object
+ ///
+ ///
+ /// A valid Json string ([.....)
+ ///
+ private int CloseBracketArray(
+ string json)
+ {
+ var countArrayBegin = 0;
+ var countArrayEnd = 0;
+ var n = 0;
+
+ do
+ {
+ if (json[n] == ArrayBegin)
+ countArrayBegin++;
+ else if (json[n] == ArrayEnd) countArrayEnd++;
+
+ n++;
+ }
+ while (countArrayBegin != countArrayEnd);
+
+ return n;
+ }
+
+ ///
+ /// Deserialize a Json Array into an object JsonArray
+ ///
+ ///
+ /// JsonArray object as a deserialized JSON array
+ ///
+ ///
+ /// valid JSON array except the brackets
+ ///
+ private JsonArray SerializeArray(
+ string array)
+ {
+ var arrayObject = new JsonArray();
+ var elements = SplitElements(array);
+
+ foreach (var item in elements)
+ if (item[0] == DoubleQuotes)
+ {
+ var withoutQuotes = item.Trim(DoubleQuotes);
+ JsonObject o = new JsonString(withoutQuotes);
+ arrayObject.AddElementToArray(o);
+ }
+ else if (item[0] == ObjectBegin)
+ {
+ var o = SerializeObject(item);
+ arrayObject.AddElementToArray(o);
+ }
+ else if (item[0] == ArrayBegin)
+ {
+ var itemArray = item.Substring(1, item.Length - 2);
+ var secondaryArray = SerializeArray(itemArray);
+ JsonObject o = secondaryArray;
+ arrayObject.AddElementToArray(o);
+ }
+ else if (item[0] == '-' ||
+ item[0] == '0' ||
+ item[0] == '1' ||
+ item[0] == '2' ||
+ item[0] == '3' ||
+ item[0] == '4' ||
+ item[0] == '5' ||
+ item[0] == '6' ||
+ item[0] == '7' ||
+ item[0] == '8' ||
+ item[0] == '9')
+ {
+ var doubleValue = Convert.ToDouble(item);
+ var floatValue = (float)doubleValue;
+ JsonObject o = new JsonNumber(floatValue);
+ arrayObject.AddElementToArray(o);
+ }
+ else if (string.Compare(
+ SafeSubString(
+ item,
+ 0,
+ 4),
+ TrueValue,
+ StringComparison.Ordinal) ==
+ 0)
+ {
+ JsonObject o = new JsonBoolean(true);
+ arrayObject.AddElementToArray(o);
+ }
+ else if (string.Compare(
+ SafeSubString(
+ item,
+ 0,
+ 5),
+ FalseValue,
+ StringComparison.Ordinal) ==
+ 0)
+ {
+ JsonObject o = new JsonBoolean(false);
+ arrayObject.AddElementToArray(o);
+ }
+ else if (string.Compare(
+ SafeSubString(
+ item,
+ 0,
+ 4),
+ NullValue,
+ StringComparison.Ordinal) ==
+ 0)
+ {
+ JsonObject o = new JsonNullable();
+ arrayObject.AddElementToArray(o);
+ }
+
+ return arrayObject;
+ }
+
+ ///
+ /// Just a safe subString operation
+ ///
+ ///
+ /// A subString of the string input parameter text
+ ///
+ ///
+ /// A string
+ ///
+ ///
+ /// index of starting
+ ///
+ ///
+ /// Length of the subString
+ ///
+ private string SafeSubString(
+ string text,
+ int start,
+ int length)
+ {
+ var safeString = start + length < text.Length ?
+ text.Substring(start, length) :
+ text.Substring(start, text.Length - start);
+
+ return safeString;
+ }
+
+ ///
+ /// Finds the next '"' to close a String field
+ ///
+ ///
+ /// The next ' " '
+ ///
+ ///
+ /// A valid JSON string
+ ///
+ ///
+ /// Index of starting
+ ///
+ private int FindNextQuote(
+ string text,
+ int index)
+ {
+ var nextQuote = text.IndexOf(DoubleQuotes, index);
+ while (text[nextQuote - 1] == BackSlash) nextQuote = text.IndexOf(DoubleQuotes, nextQuote + 1);
+
+ return nextQuote;
+ }
+
+ ///
+ /// Splits the elements of an Array
+ ///
+ ///
+ /// The elements in an array of Strings
+ ///
+ ///
+ ///
+ private string[] SplitElements(
+ string arrayText)
+ {
+ var n = 0;
+ var doubleQuotesCounter = 0;
+ var objectBeginCounter = 0;
+ var objectEndCounter = 0;
+ var arrayBeginCounter = 0;
+ var arrayEndCounter = 0;
+ var previousCommaIndex = 0;
+ var oneElement = true;
+ var textSplit = new List();
+
+ while (n <= arrayText.Length - 1)
+ if (arrayText[n] == DoubleQuotes &&
+ arrayText[n - 1] != BackSlash)
+ {
+ doubleQuotesCounter++;
+ n++;
+ }
+ else if (arrayText[n] == ObjectBegin)
+ {
+ objectBeginCounter++;
+ n++;
+ }
+ else if (arrayText[n] == ObjectEnd)
+ {
+ objectEndCounter++;
+ n++;
+ }
+ else if (arrayText[n] == ArrayBegin)
+ {
+ arrayBeginCounter++;
+ n++;
+ }
+ else if (arrayText[n] == ArrayEnd)
+ {
+ arrayEndCounter++;
+ n++;
+ }
+ else if (arrayText[n] == Comma &&
+ doubleQuotesCounter % 2 == 0 &&
+ objectBeginCounter == objectEndCounter &&
+ arrayBeginCounter == arrayEndCounter)
+ {
+ textSplit.Add(arrayText.Substring(previousCommaIndex, n - previousCommaIndex));
+ previousCommaIndex = n + 1;
+ n++;
+ oneElement = false;
+ }
+ else
+ {
+ n++;
+ }
+
+ textSplit.Add(
+ oneElement ? arrayText : arrayText.Substring(previousCommaIndex, arrayText.Length - previousCommaIndex));
+
+ var textSplitArray = textSplit.ToArray();
+ return textSplitArray;
+ }
+}
\ No newline at end of file
diff --git a/Kavenegar.Core/Kavenegar.Core.csproj b/Kavenegar.Core/Kavenegar.Core.csproj
index d29e2a9..752fb60 100644
--- a/Kavenegar.Core/Kavenegar.Core.csproj
+++ b/Kavenegar.Core/Kavenegar.Core.csproj
@@ -1,33 +1,34 @@
-
- 1.0.5
-
- netstandard1.2;netstandard1.3;netstandard1.4;netstandard1.5;netstandard1.6;netstandard2.0;
- A cross-platform library for the kavenegar sms provider; written in C#
- Kavenegar.Core
- Kavenegar.Core
- Kavenegar.Core
-
- KavenegarDotNetCore
- Kavenegar Core kavenegar.Core kavenegarCore Sms
-
- https://github.com/kavenegar/Kavenegar.Core
+
+ 1.0.5
- Library
- true
- true
- HadiEskandari Ardavan HosseinRezaei Mberneti MohammadAmirriazi
- git
- https://github.com/kavenegar/Kavenegar.Core
- Kavenegar
-
- true
+ net6.0;net6.0-windows;netstandard1.2;netstandard1.3;netstandard1.4;netstandard1.5;netstandard1.6;netstandard2.0
+ A cross-platform library for the kavenegar sms provider; written in C#
+ Kavenegar.Core
+ Kavenegar.Core
-
+ KavenegarDotNetCore
+ Kavenegar Core kavenegar.Core kavenegarCore Sms
+
+ https://github.com/kavenegar/Kavenegar.Core
-
-
-
+ Library
+ true
+ true
+ HadiEskandari Ardavan HosseinRezaei Mberneti MohammadAmirriazi
+ git
+ https://github.com/kavenegar/Kavenegar.Core
+ Kavenegar
+
+ true
+ 11
+ enable
+
+
+
+
+
+
diff --git a/Kavenegar.Core/KavenegarApi.cs b/Kavenegar.Core/KavenegarApi.cs
index abadf94..0f76bbf 100644
--- a/Kavenegar.Core/KavenegarApi.cs
+++ b/Kavenegar.Core/KavenegarApi.cs
@@ -1,619 +1,1009 @@
using System;
using System.Collections.Generic;
-using System.IO;
using System.Linq;
using System.Net;
-using System.Text;
+using System.Net.Http;
+using System.Threading.Tasks;
using Kavenegar.Core.Exceptions;
using Kavenegar.Core.Models;
using Kavenegar.Core.Models.Enums;
using Kavenegar.Core.Utils;
-using System.Net.Http;
using Newtonsoft.Json;
-using System.Threading.Tasks;
-namespace Kavenegar
+namespace Kavenegar.Core;
+
+public class KavenegarApi : IKavenegarApi
{
- internal class ReturnResult
- {
- public Result @Return { get; set; }
- public object entries { get; set; }
- }
+ private const string ApiPath = "{0}/{1}.{2}";
+ private const string BaseUrl = "http://api.kavenegar.com/v1";
+ private static HttpClient _client = null!;
- internal class Result
+ public KavenegarApi(
+ string apikey)
{
- public int status { get; set; }
- public string message { get; set; }
+ ApiKey = apikey;
+
+ _client = new HttpClient
+ {
+ BaseAddress = new Uri($"{BaseUrl}/{ApiKey}/")
+ };
}
- internal class ReturnSend
+ public string ApiKey { set; get; }
+
+ private string GetApiPath(
+ string @base,
+ string method,
+ string output)
{
- public Result @Return { get; set; }
- public List entries { get; set; }
+ return string.Format(
+ ApiPath,
+ @base,
+ method,
+ output);
}
- internal class ReturnStatus
+ private async Task Execute(
+ string path,
+ Dictionary @params)
{
- public Result result { get; set; }
- public List entries { get; set; }
+ var nvc = @params?.Select(x => new KeyValuePair(x.Key, x.Value?.ToString()));
+
+ var postdata = new FormUrlEncodedContent(nvc);
+
+ var response = await _client.PostAsync(path, postdata);
+ var responseBody = await response.Content.ReadAsStringAsync();
+
+ var result = JsonConvert.DeserializeObject(responseBody);
+
+ if (response.StatusCode != HttpStatusCode.OK)
+ throw new ApiException(result.Return.message, result.Return.status);
+
+ return responseBody;
}
- internal class ReturnStatusLocalMessageId
+ public async Task> Send(
+ string sender,
+ List receptor,
+ string message)
{
- public Result result { get; set; }
- public List entries { get; set; }
+ return await Send(
+ sender,
+ receptor,
+ message,
+ MessageType.MobileMemory,
+ DateTime.MinValue);
}
- internal class ReturnReceive
+ public async Task Send(
+ string sender,
+ string receptor,
+ string message)
{
- public Result result { get; set; }
- public List entries { get; set; }
+ return await Send(
+ sender,
+ receptor,
+ message,
+ MessageType.MobileMemory,
+ DateTime.MinValue);
}
- internal class ReturnCountOutbox
+ public async Task Send(
+ string sender,
+ string receptor,
+ string message,
+ MessageType type,
+ DateTime date)
{
- public Result result { get; set; }
- public List entries { get; set; }
+ var receptors = new List
+ {
+ receptor
+ };
+ return (await Send(
+ sender,
+ receptors,
+ message,
+ type,
+ date))[0];
}
- internal class ReturnCountInbox
+ public async Task> Send(
+ string sender,
+ List receptor,
+ string message,
+ MessageType type,
+ DateTime date)
{
- public Result result { get; set; }
- public List entries { get; set; }
-
+ return await Send(
+ sender,
+ receptor,
+ message,
+ type,
+ date,
+ null);
}
- internal class ReturnCountPostalCode
+ public async Task Send(
+ string sender,
+ string receptor,
+ string message,
+ MessageType type,
+ DateTime date,
+ string localId)
{
- public Result result { get; set; }
- public List entries { get; set; }
+ var receptors = new List
+ {
+ receptor
+ };
+ var localIds = new List
+ {
+ localId
+ };
+ return (await Send(
+ sender,
+ receptors,
+ message,
+ type,
+ date,
+ localIds))[0];
}
- internal class ReturnAccountInfo
+ public async Task Send(
+ string sender,
+ string receptor,
+ string message,
+ string localId)
{
- public Result result { get; set; }
- public AccountInfoResult entries { get; set; }
+ return await Send(
+ sender,
+ receptor,
+ message,
+ MessageType.MobileMemory,
+ DateTime.MinValue,
+ localId);
}
- internal class ReturnAccountConfig
+ public async Task> Send(
+ string sender,
+ List receptors,
+ string message,
+ string localId)
{
- public Result result { get; set; }
- public AccountConfigResult entries { get; set; }
+ var localIds = new List();
+ for (var i = 0; i <= receptors.Count - 1; i++) localIds.Add(localId);
+ return await Send(
+ sender,
+ receptors,
+ message,
+ MessageType.MobileMemory,
+ DateTime.MinValue,
+ localIds);
}
- public class KavenegarApi
+ public async Task> Send(
+ string sender,
+ List receptor,
+ string message,
+ MessageType type,
+ DateTime date,
+ List localIds)
{
- private string _apikey;
- private static HttpClient _client;
- private int _returnCode = 200;
- private string _returnMessage = "";
- private const string Apipath = "{0}/{1}.{2}";
- private const string BaseUrl = "http://api.kavenegar.com/v1";
- public KavenegarApi(string apikey)
+ var path = GetApiPath(
+ "sms",
+ "send",
+ "json");
+ var param = new Dictionary
{
- _apikey = apikey;
-
- _client = new HttpClient
{
- BaseAddress = new Uri($"{BaseUrl}/{_apikey}/")
- };
-
- }
-
- public string ApiKey
- {
- set => _apikey = value;
- get => _apikey;
- }
+ "sender", WebUtility.HtmlEncode(sender)
+ },
+ {
+ "receptor", WebUtility.HtmlEncode(string.Join(",", receptor))
+ },
+ {
+ "message", message
+ },
+ {
+ "type", (int)type
+ },
+ {
+ "date", date == DateTime.MinValue ? 0 : DateHelper.DateTimeToUnixTimestamp(date)
+ }
+ };
+ if (localIds != null &&
+ localIds.Count > 0)
+ param.Add("localId", string.Join(",", localIds));
+ var responseBody = await Execute(path, param);
+ var l = JsonConvert.DeserializeObject(responseBody);
+ return l.entries;
+ }
- public int ReturnCode
- {
- get { return _returnCode; }
+ public async Task> SendArray(
+ List senders,
+ List receptors,
+ List messages)
+ {
+ var types = new List();
+ for (var i = 0; i <= senders.Count - 1; i++) types.Add(MessageType.MobileMemory);
+ return await SendArray(
+ senders,
+ receptors,
+ messages,
+ types,
+ DateTime.MinValue,
+ null);
+ }
- }
+ public async Task> SendArray(
+ string sender,
+ List receptors,
+ List messages,
+ MessageType type,
+ DateTime date)
+ {
+ var senders = new List();
+ for (var i = 0; i < receptors.Count; i++) senders.Add(sender);
+ var types = new List();
+ for (var i = 0; i <= senders.Count - 1; i++) types.Add(MessageType.MobileMemory);
+ return await SendArray(
+ senders,
+ receptors,
+ messages,
+ types,
+ date,
+ null);
+ }
- public string ReturnMessage
- {
- get { return _returnMessage; }
+ public async Task> SendArray(
+ string sender,
+ List receptors,
+ List messages,
+ MessageType type,
+ DateTime date,
+ string localMessageIds)
+ {
+ var senders = new List();
+ for (var i = 0; i < receptors.Count; i++) senders.Add(sender);
+ var types = new List();
+ for (var i = 0; i <= senders.Count - 1; i++) types.Add(MessageType.MobileMemory);
+ return await SendArray(
+ senders,
+ receptors,
+ messages,
+ types,
+ date,
+ new List
+ {
+ localMessageIds
+ });
+ }
- }
+ public async Task> SendArray(
+ string sender,
+ List receptors,
+ List messages,
+ string localMessageId)
+ {
+ var senders = new List();
+ for (var i = 0; i < receptors.Count; i++) senders.Add(sender);
+
+ return await SendArray(
+ senders,
+ receptors,
+ messages,
+ localMessageId);
+ }
- private string GetApiPath(string _base, string method, string output)
- {
- return string.Format(Apipath, _base, method, output);
- }
+ public async Task> SendArray(
+ List senders,
+ List receptors,
+ List messages,
+ string localMessageId)
+ {
+ var types = new List();
+ for (var i = 0; i <= receptors.Count - 1; i++) types.Add(MessageType.MobileMemory);
+ var localMessageIds = new List();
+ for (var i = 0; i <= receptors.Count - 1; i++) localMessageIds.Add(localMessageId);
+ return await SendArray(
+ senders,
+ receptors,
+ messages,
+ types,
+ DateTime.MinValue,
+ localMessageIds);
+ }
- private async Task Execute(string path, Dictionary _params)
+ public async Task> SendArray(
+ List senders,
+ List receptors,
+ List messages,
+ List types,
+ DateTime date,
+ List localMessageIds)
+ {
+ var path = GetApiPath(
+ "sms",
+ "sendarray",
+ "json");
+ var jsonSenders = JsonConvert.SerializeObject(senders);
+ var jsonReceptors = JsonConvert.SerializeObject(receptors);
+ var jsonMessages = JsonConvert.SerializeObject(messages);
+ var jsonTypes = JsonConvert.SerializeObject(types);
+ var param = new Dictionary
{
- var nvc = _params?.Select(x => new KeyValuePair(x.Key, x.Value?.ToString()));
-
- var postdata = new FormUrlEncodedContent(nvc);
-
- var response = await _client.PostAsync(path, postdata);
- var responseBody = await response.Content.ReadAsStringAsync();
-
- // System.Diagnostics.Debug.WriteLine(responseBody);
-
- try
{
- var result = JsonConvert.DeserializeObject(responseBody);
-
- if (response.StatusCode != HttpStatusCode.OK)
- throw new ApiException(result.Return.message, result.Return.status);
-
- return responseBody;
- }
- catch (HttpException)
+ "message", jsonMessages
+ },
+ {
+ "sender", jsonSenders
+ },
+ {
+ "receptor", jsonReceptors
+ },
+ {
+ "type", jsonTypes
+ },
{
- throw;
+ "date", date == DateTime.MinValue ? 0 : DateHelper.DateTimeToUnixTimestamp(date)
}
- }
- public async Task> Send(string sender, List receptor, string message)
- {
- return await Send(sender, receptor, message, MessageType.MobileMemory, DateTime.MinValue);
- }
+ };
+ if (localMessageIds != null &&
+ localMessageIds.Count > 0)
+ param.Add("localMessageIds", string.Join(",", localMessageIds));
+
+ var responsebody = await Execute(path, param);
+ var l = JsonConvert.DeserializeObject(responsebody);
+ if (l.entries == null) return new List();
+ return l.entries;
+ }
- public async Task Send(string sender, String receptor, string message)
- {
- return await Send(sender, receptor, message, MessageType.MobileMemory, DateTime.MinValue);
- }
- public async Task Send(string sender, string receptor, string message, MessageType type, DateTime date)
- {
- List receptors = new List { receptor };
- return (await Send(sender, receptors, message, type, date))[0];
- }
- public async Task> Send(string sender, List receptor, string message, MessageType type, DateTime date)
- {
- return await Send(sender, receptor, message, type, date, null);
- }
- public async Task Send(string sender, string receptor, string message, MessageType type, DateTime date, string localid)
- {
- var receptors = new List { receptor };
- var localids = new List { localid };
- return (await Send(sender, receptors, message, type, date, localids))[0];
- }
- public async Task Send(string sender, string receptor, string message, string localid)
- {
- return await Send(sender, receptor, message, MessageType.MobileMemory, DateTime.MinValue, localid);
- }
- public async Task> Send(string sender, List receptors, string message, string localid)
+ public async Task> Status(
+ List messageIds)
+ {
+ var path = GetApiPath(
+ "sms",
+ "status",
+ "json");
+ var param = new Dictionary
{
- List localids = new List();
- for (var i = 0; i <= receptors.Count - 1; i++)
{
- localids.Add(localid);
+ "messageId", string.Join(",", messageIds)
}
- return await Send(sender, receptors, message, MessageType.MobileMemory, DateTime.MinValue, localids);
- }
- public async Task> Send(string sender, List receptor, string message, MessageType type, DateTime date, List localids)
- {
- var path = GetApiPath("sms", "send", "json");
- var param = new Dictionary
- {
- {"sender", System.Net.WebUtility.HtmlEncode(sender)},
- {"receptor", System.Net.WebUtility.HtmlEncode(StringHelper.Join(",", receptor.ToArray()))},
- {"message", message},
- {"type", (int) type},
- {"date", date == DateTime.MinValue ? 0 : DateHelper.DateTimeToUnixTimestamp(date)}
};
- if (localids != null && localids.Count > 0)
- {
- param.Add("localid", StringHelper.Join(",", localids.ToArray()));
- }
- var responseBody = await Execute(path, param);
- var l = JsonConvert.DeserializeObject(responseBody);
- return l.entries;
- }
+ var responsebody = await Execute(path, param);
+ var l = JsonConvert.DeserializeObject(responsebody);
+ if (l.entries == null) return new List();
+ return l.entries;
+ }
- public async Task> SendArray(List senders, List receptors, List messages)
+ public async Task Status(
+ string messageId)
+ {
+ var ids = new List
{
- var types = new List();
- for (var i = 0; i <= senders.Count - 1; i++)
- {
- types.Add(MessageType.MobileMemory);
- }
- return await SendArray(senders, receptors, messages, types, DateTime.MinValue, null);
- }
+ messageId
+ };
+ var result = await Status(ids);
+ return result.Count == 1 ? result[0] : null;
+ }
- public async Task> SendArray(string sender, List receptors, List messages, MessageType type, DateTime date)
+ public async Task> StatusLocalMessageId(
+ List messageIds)
+ {
+ var path = GetApiPath(
+ "sms",
+ "statuslocalmessageid",
+ "json");
+ var param = new Dictionary
{
- var senders = new List();
- for (var i = 0; i < receptors.Count; i++)
{
- senders.Add(sender);
+ "localId", string.Join(",", messageIds)
}
- var types = new List();
- for (var i = 0; i <= senders.Count - 1; i++)
+ };
+ var responsebody = await Execute(path, param);
+ var l = JsonConvert.DeserializeObject(responsebody);
+ return l.entries;
+ }
+
+ public async Task StatusLocalMessageId(
+ string messageId)
+ {
+ var result = await StatusLocalMessageId(
+ new List
{
- types.Add(MessageType.MobileMemory);
- }
- return await SendArray(senders, receptors, messages, types, date, null);
- }
+ messageId
+ });
+ return result.Count == 1 ? result[0] : null;
+ }
- public async Task> SendArray(string sender, List receptors, List messages, MessageType type, DateTime date, string localmessageids)
+ public async Task> Select(
+ List messageIds)
+ {
+ var path = GetApiPath(
+ "sms",
+ "select",
+ "json");
+ var param = new Dictionary
{
- var senders = new List();
- for (var i = 0; i < receptors.Count; i++)
{
- senders.Add(sender);
+ "messageId", string.Join(",", messageIds)
}
- List types = new List();
- for (var i = 0; i <= senders.Count - 1; i++)
- {
- types.Add(MessageType.MobileMemory);
- }
- return await SendArray(senders, receptors, messages, types, date, new List() { localmessageids });
- }
+ };
+ var responsebody = await Execute(path, param);
+ var l = JsonConvert.DeserializeObject(responsebody);
+ if (l.entries == null) return new List();
+ return l.entries;
+ }
- public async Task> SendArray(string sender, List receptors, List messages, string localmessageid)
+ public async Task Select(
+ string messageId)
+ {
+ var ids = new List
{
- List senders = new List();
- for (var i = 0; i < receptors.Count; i++)
- {
- senders.Add(sender);
- }
+ messageId
+ };
+ var result = await Select(ids);
+ return result.Count == 1 ? result[0] : null;
+ }
+
+ public async Task> SelectOutbox(
+ DateTime startDate)
+ {
+ return await SelectOutbox(startDate, DateTime.MaxValue);
+ }
- return await SendArray(senders, receptors, messages, localmessageid);
- }
+ public async Task> SelectOutbox(
+ DateTime startDate,
+ DateTime endDate)
+ {
+ return await SelectOutbox(
+ startDate,
+ endDate,
+ null);
+ }
- public async Task> SendArray(List senders, List receptors, List messages, string localmessageid)
+ public async Task