Пример #1
0
        public static List <JSONDocument> Serialize <T>(List <T> documents)
        {
            if (typeof(T) == typeof(JSONDocument))
            {
                return(documents as List <JSONDocument>);
            }

            List <JSONDocument> JSONDocuments;

            JSONDocuments = new List <JSONDocument>();
            JSONDocument jdoc;

            foreach (T document in documents)
            {
                if (document != null)
                {
                    string str = JsonConvert.SerializeObject(document);
                    jdoc = (JSONDocument)JSONDocument.Parse(str);
                    JSONDocuments.Add(jdoc);
                }
                else
                {
                    throw new ArgumentException("document cannot be null");
                }
            }
            return(JSONDocuments);
        }
Пример #2
0
        public static ArrayList ParseArray(JToken value)
        {
            ArrayList arrayList = new ArrayList();

            foreach (var token in value)
            {
                if (token is JObject)
                {
                    arrayList.Add(JSONDocument.Parse(token.ToString()));
                }
                else if (token is JArray)
                {
                    arrayList.Add(ParseArray(token));
                }
                else if (token is JValue)
                {
                    object tokenValue = ((JValue)token).Value;
                    Type   type;
                    if (!IsSupportedParameterType(tokenValue, out type))
                    {
                        throw new NotSupportedException(string.Format("Type {0} is not supported", type.Name));
                    }
                    arrayList.Add(((JValue)token).Value);
                }
                else
                {
                    throw new NotSupportedException(string.Format("Type {0} is not supported", token.GetType().FullName));
                }
            }
            return(arrayList);
        }
Пример #3
0
        private string GetResponseBody(HttpWebResponse response)
        {
            //Get Response Body.
            string responseJson = string.Empty;
            Stream dataStream   = response.GetResponseStream();

            if (dataStream != null)
            {
                var    reader             = new StreamReader(dataStream);
                string responseFromServer = reader.ReadToEnd();

                //Response Body will be in JSON Form.
                IJSONDocument responseDocument = JSONDocument.Parse(responseFromServer);
                if (responseDocument != null && responseDocument.Contains("value"))
                {
                    responseJson = responseDocument.GetString("value");  //Data will be in value
                }
                reader.Close();
            }
            if (dataStream != null)
            {
                dataStream.Close();
            }
            return(responseJson);
        }
Пример #4
0
        //private JsonSerializerSettings _settings = new JsonSerializerSettings();
        //public JsonSerializer()
        //{
        //    _settings.TypeNameHandling = TypeNameHandling.Objects;
        //}

        public static JSONDocument Serialize <T>(T document)
        {
            if (document == null)
            {
                return(null);
            }
            if (typeof(T) == typeof(JSONDocument))
            {
                return((JSONDocument)(object)document);
            }
            return((JSONDocument)JSONDocument.Parse(JsonConvert.SerializeObject(document)));
        }
Пример #5
0
        public DeleteDocumentsOperation(Alachisoft.NosDB.Common.Protobuf.Command command) : base(command.ToBuilder())
        {
            _deleteCommand = command.DeleteDocumentsCommand.ToBuilder();
            _documentIds   = new List <IJSONDocument>();

            foreach (string document in _deleteCommand.DocumentIdsList)
            {
                _documentIds.Add(JSONDocument.Parse(document));
            }

            base.Message = this;
        }
Пример #6
0
        public static JSONDocument Serialize <T>(T instance, JsonConverter[] converters)
        {
            if (instance == null)
            {
                return(null);
            }
            if (typeof(T) == typeof(JSONDocument))
            {
                return((JSONDocument)(object)instance);
            }

            return((JSONDocument)JSONDocument.Parse(JsonConvert.SerializeObject(instance, converters)));
        }
Пример #7
0
        public static IJSONDocument Serialize(object document)
        {
            var serialize = document as IJSONDocument;

            if (serialize != null)
            {
                return(serialize);
            }

            string str = JsonConvert.SerializeObject(document);

            return(JSONDocument.Parse(str));
        }
Пример #8
0
        public static IList <IParameter> GetParameterList(IList <Protobuf.Parameter> paramList)
        {
            IList <IParameter> parameterList = new List <IParameter>();

            foreach (Protobuf.Parameter param in paramList)
            {
                try
                {
                    switch ((ParameterType)param.JsonDataType)
                    {
                    case ParameterType.NULL:
                        parameterList.Add(new Parameter(param.Attribute, null));
                        break;

                    case ParameterType.BOOLEAN:
                        parameterList.Add(new Parameter(param.Attribute, bool.Parse(param.Value)));
                        break;

                    case ParameterType.DATETIME:
                        parameterList.Add(new Parameter(param.Attribute, DateTime.Parse(param.Value)));
                        break;

                    case ParameterType.STRING:
                        parameterList.Add(new Parameter(param.Attribute, param.Value));
                        break;

                    case ParameterType.LONG:
                        parameterList.Add(new Parameter(param.Attribute, long.Parse(param.Value)));
                        break;

                    case ParameterType.DOUBLE:
                        parameterList.Add(new Parameter(param.Attribute, double.Parse(param.Value)));
                        break;

                    case ParameterType.ARRAY:
                        parameterList.Add(new Parameter(param.Attribute,
                                                        Alachisoft.NosDB.Common.JSON.JsonDocumentUtil.ParseArray(JsonConvert.DeserializeObject <JArray>(param.Value))));
                        break;

                    default:
                        parameterList.Add(new Parameter(param.Attribute, JSONDocument.Parse(param.Value)));
                        break;
                    }
                }
                catch (NotSupportedException ex)
                {
                    throw new QuerySystemException(ErrorCodes.Query.PARAMETER_NOT_SUPPORTED, new[] { param.Attribute, ex.Message });
                }
            }
            return(parameterList);
        }
Пример #9
0
        public GetChunkResponse(Alachisoft.NosDB.Common.Protobuf.Response response) : base(response.ToBuilder())
        {
            _getChunkResponse = response.GetChunkResponse.ToBuilder();
            _dataChunkBuilder = new DataChunk();

            _dataChunkBuilder.ChunkId     = _getChunkResponse.DataChunk.ChunkId;
            _dataChunkBuilder.ReaderUID   = _getChunkResponse.DataChunk.ReaderUId;
            _dataChunkBuilder.IsLastChunk = _getChunkResponse.DataChunk.IsLastChunk;

            foreach (string document in _getChunkResponse.DataChunk.DocumentsList)
            {
                _dataChunkBuilder.Documents.Add(JSONDocument.Parse(document));
            }

            base.ResponseMessage = this;
        }
Пример #10
0
        public void TestMethod1()
        {
            var source = @"
 [
  [""Jack Smith"",""22.33.44.55:6666""],
  [""John Doe"",""22.33.44.11:6666""]
  ]			
			
			"            ;

            JSONDocument.Parse(source,
                               new JSONDocument.ParseArguments
            {
                FoundArray = a =>
                {
                    // before root element
                    a.Invoke(
                        new JSONDocument.ParseArguments
                    {
                        FoundArray = b =>
                        {
                            // inner string list starting

                            // whenever we find a string...

                            b(
                                new JSONDocument.ParseArguments
                            {
                                FoundString =
                                    text =>
                                {
                                    Console.WriteLine(text);
                                }
                            }
                                );

                            // inner string list done
                        }
                    }
                        );
                    // after root element
                }
            }
                               );
        }
Пример #11
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
        {
            JObject jObject = JObject.Load(reader);

            return((JSONDocument)JSONDocument.Parse(jObject.ToString()));
        }
Пример #12
0
        public void GetNextChunk(string requestUri, string readerId, int lastChunkId)
        {
            //Create Web Request
            WebRequest request = WebRequest.Create(requestUri);

            //Set Request Method to POST. POST is used for GetChunk also.
            request.Method = "POST";

            //Information Required to GetNextChunk.
            var jsonDocument = new JSONDocument {
                { "ReaderId", readerId }, { "LastChunkId", lastChunkId }
            };
            string postData = jsonDocument.ToString();

            byte[] byteArray = Encoding.UTF8.GetBytes(postData);

            //Content-Type must be set to application/getchunk.
            request.ContentType   = "application/getchunk";
            request.ContentLength = byteArray.Length;

            //Get Request stream and write data.. Data written in the stream is a body of Request.
            Stream dataStream = request.GetRequestStream();

            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();

            WebResponse response;

            try
            {
                //Send request and get HTTP response.
                response = request.GetResponse();
            }
            catch (WebException exception)
            {
                PrintErrorResponse(exception);
                return;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }

            dataStream = response.GetResponseStream();
            if (dataStream != null)
            {
                var    reader             = new StreamReader(dataStream);
                string responseFromServer = reader.ReadToEnd();
                var    responseDocument   = JSONDocument.Parse(responseFromServer);

                if (responseDocument != null)
                {
                    string temp      = responseDocument.GetString("value");              //Query Result Data in JSON Array
                    var    customers = JsonConvert.DeserializeObject <Customer[]>(temp); //Deserialize JSON Array to Customer Array.
                    _numberOfRecords += customers.Length;
                    //Perform Operations
                }

                reader.Close();
            }
            if (dataStream != null)
            {
                dataStream.Close();
            }

            //Retrieve Query Result Set Information From Response Header.
            //This information is required to GetNextChunk.
            readerId = response.Headers.Get("ReaderId");
            string isLastChunk = response.Headers.Get("IsLastChunk");
            string chunkId     = response.Headers.Get("ChunkId");

            response.Close();

            if (isLastChunk.Equals("False", StringComparison.OrdinalIgnoreCase)) //Check if this is a last chunk of data.
            {
                //If IsLastChunk is false then GetNextChunk of Data.
                //Else Complete Data is retrieved.
                GetNextChunk(requestUri, readerId, Convert.ToInt32(chunkId));
            }
        }
Пример #13
0
        public int ExecuteQuery(string requestUri, string queryText, List <IParameter> parameter)
        {
            _numberOfRecords = 0;
            //Create Web Request
            WebRequest request = WebRequest.Create(requestUri);

            //Set Request Method to POST. POST is used to Execute Query also.
            request.Method = "POST";

            var query = new Query {
                QueryText = queryText, Parameters = parameter
            };

            //Json Serialize Query
            string serializedQuery = JsonConvert.SerializeObject(query);

            string postData = serializedQuery;

            byte[] byteArray = Encoding.UTF8.GetBytes(postData);

            //Content-Type must be set to application/nosquery.
            request.ContentType   = "application/nosquery";
            request.ContentLength = byteArray.Length;

            //Get Request stream and write data..
            Stream dataStream = request.GetRequestStream();

            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();

            WebResponse response;

            try
            {
                //Send request and get HTTP response.
                response = request.GetResponse();
            }
            catch (WebException exception)
            {
                PrintErrorResponse(exception);
                return(0);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(0);
            }

            //Get Response Stream to Read Data from Response Body.
            dataStream = response.GetResponseStream();

            if (dataStream != null)
            {
                var    reader             = new StreamReader(dataStream);
                string responseFromServer = reader.ReadToEnd();
                var    responseDocument   = JSONDocument.Parse(responseFromServer);

                if (responseDocument != null)
                {
                    string serializedData = responseDocument.GetString("value");                        //Query Result Data in JSON Array
                    var    customers      = JsonConvert.DeserializeObject <Customer[]>(serializedData); //Deserialize JSON Array to Customer Array.
                    _numberOfRecords += customers.Length;
                    //Perform Operations
                }
                reader.Close();
            }
            if (dataStream != null)
            {
                dataStream.Close();
            }

            //Retrieve Query Result Set Information From Response Header.
            //This information is required to GetNextChunk.
            string readerId    = response.Headers.Get("ReaderId");
            string isLastChunk = response.Headers.Get("IsLastChunk");
            string chunkId     = response.Headers.Get("ChunkId");

            response.Close();

            if (isLastChunk.Equals("False", StringComparison.OrdinalIgnoreCase)) //Check if this is a last chunk of data.
            {
                //If IsLastChunk is false then GetNextChunk of Data.
                //Else Complete Data is retrieved.
                GetNextChunk(requestUri, readerId, Convert.ToInt32(chunkId));
            }
            return(_numberOfRecords);
        }