Example #1
0
        public static DbValue FromJson(JsonObject json)
        {
            // {"N":"225"}
            // {"S":"Hello"}
            // {"B":"dmFsdWU="}
            // {"SS": ["Keneau", "Alexis", "John"]}

            // { "L": [ { "N": "1" }, { "N":"2" } ]

            var property = json.First();

            switch (property.Key)
            {
            case "B": return(new DbValue(property.Value.ToString(), DbValueType.B));

            case "N": return(new DbValue(property.Value.ToString(), DbValueType.N));

            case "S": return(new DbValue(property.Value.ToString(), DbValueType.S));

            case "BOOL": return(new DbValue((bool)property.Value, DbValueType.BOOL));

            case "BS": return(new DbValue(((JsonArray)property.Value).ToArrayOf <string>(), DbValueType.BS));

            case "NS": return(new DbValue(((JsonArray)property.Value).ToArrayOf <string>(), DbValueType.NS));

            case "SS": return(new DbValue(((JsonArray)property.Value).ToArrayOf <string>(), DbValueType.SS));

            case "L": return(new DbValue(GetListValues((JsonArray)property.Value).ToArray()));

            case "M": return(new DbValue(AttributeCollection.FromJson((JsonObject)property.Value)));

            default: throw new Exception("Unexpected value type:" + property.Key);
            }
        }
Example #2
0
        public static QueryResult FromJson(JsonObject json)
        {
            var result = new QueryResult {
                Count = (int)json["Count"]
            };

            if (json.ContainsKey("ConsumedCapacity"))
            {
                result.ConsumedCapacity = ConsumedCapacity.FromJson((JsonObject)json["ConsumedCapacity"]);
            }

            if (json.ContainsKey("LastEvaluatedKey"))
            {
                result.LastEvaluatedKey = AttributeCollection.FromJson((JsonObject)json["LastEvaluatedKey"]);
            }

            if (json.ContainsKey("Items"))
            {
                foreach (var item in (JsonArray)json["Items"])
                {
                    result.Items.Add(AttributeCollection.FromJson((JsonObject)item));
                }
            }

            return(result);
        }
Example #3
0
        public static BatchGetItemResult FromJson(JsonObject json)
        {
            var result = new BatchGetItemResult();

            var items = new List <AttributeCollection>();

            if (json.ContainsKey("ConsumedCapacity")) // Array
            {
                foreach (var item in (JsonArray)json["ConsumedCapacity"])
                {
                    var unit = ConsumedCapacity.FromJson((JsonObject)item);

                    // TODO
                }
            }

            if (json.ContainsKey("Responses"))
            {
                foreach (var tableEl in (JsonObject)json["Responses"])
                {
                    var table = new TableItems(tableEl.Key);

                    foreach (var item in (JsonArray)tableEl.Value)
                    {
                        table.Add(AttributeCollection.FromJson((JsonObject)item));
                    }

                    result.Responses.Add(table);
                }
            }

            /*
             *          if (json.ContainsKey("UnprocessedKeys"))
             *          {
             *                  foreach (var tableEl in (JsonObject)json["UnprocessedKeys"])
             *                  {
             *                          var tableName = tableEl.Key;
             *
             *                          foreach (var keyEl in (XArray) tableEl.Value["Keys"])
             *                          {
             *                                  var attributes = new KeyValuePair<string, object>();
             *
             *                                  foreach (var prop in (JsonObject)keyEl)
             *                                  {
             *                                          var name = prop.Key;
             *                                          var value = DbValue.FromJson((JsonObject)prop.Value);
             *                                  }
             *
             *                                  var key = new Key(attributes);
             *                          }
             *                  }
             *          }
             */

            return(result);
        }
Example #4
0
        public static DeleteItemResult FromJson(JsonObject json)
        {
            var result = new DeleteItemResult();

            if (json.ContainsKey("ConsumedCapacity"))
            {
                result.ConsumedCapacity = ConsumedCapacity.FromJson((JsonObject)json["ConsumedCapacity"]);
            }

            if (json.ContainsKey("Attributes"))
            {
                result.Attributes = AttributeCollection.FromJson((JsonObject)json["Attributes"]);
            }

            return(result);
        }
Example #5
0
        public static DeleteItemResult FromJson(JsonObject json)
        {
            var result = new DeleteItemResult();

            if (json.TryGetValue("ConsumedCapacity", out var consumedCapacityNode))
            {
                result.ConsumedCapacity = ConsumedCapacity.FromJson((JsonObject)consumedCapacityNode);
            }

            if (json.TryGetValue("Attributes", out var attributesNode))
            {
                result.Attributes = AttributeCollection.FromJson((JsonObject)attributesNode);
            }

            return(result);
        }
Example #6
0
        public static GetItemResult FromJson(JsonObject json)
        {
            var result = new GetItemResult();

            if (json.ContainsKey("ConsumedCapacity"))
            {
                result.ConsumedCapacity = ConsumedCapacity.FromJson((JsonObject)json["ConsumedCapacity"]);
            }

            if (json.ContainsKey("Item"))
            {
                result.Item = AttributeCollection.FromJson((JsonObject)json["Item"]);
            }

            return(result);
        }
Example #7
0
        public static DeleteItemResult FromJson(JsonObject json)
        {
            AttributeCollection?attributes       = null;
            ConsumedCapacity?   consumedCapacity = null;

            if (json.TryGetValue("Attributes", out var attributesNode))
            {
                attributes = AttributeCollection.FromJson((JsonObject)attributesNode);
            }

            if (json.TryGetValue("ConsumedCapacity", out var consumedCapacityNode))
            {
                consumedCapacity = consumedCapacityNode.As <ConsumedCapacity>();
            }

            return(new DeleteItemResult(attributes !, consumedCapacity));
        }
Example #8
0
        public static GetItemResult FromJson(JsonObject json)
        {
            AttributeCollection?item             = null;
            ConsumedCapacity?   consumedCapacity = null;

            if (json.TryGetValue("ConsumedCapacity", out var consumedCapacityNode))
            {
                consumedCapacity = consumedCapacityNode.As <ConsumedCapacity>();
            }

            if (json.TryGetValue("Item", out var itemNode))
            {
                item = AttributeCollection.FromJson((JsonObject)itemNode);
            }

            return(new GetItemResult(item !, consumedCapacity));
        }
Example #9
0
        public static GetItemResult FromJson(JsonObject json)
        {
            var result = new GetItemResult();


            if (json.TryGetValue("ConsumedCapacity", out var consumedCapacityNode))
            {
                result.ConsumedCapacity = ConsumedCapacity.FromJson((JsonObject)consumedCapacityNode);
            }

            if (json.TryGetValue("Item", out var itemNode))
            {
                result.Item = AttributeCollection.FromJson((JsonObject)itemNode);
            }

            return(result);
        }
Example #10
0
        public static PutItemResult FromJson(JsonObject json)
        {
            AttributeCollection attributes       = null;
            ConsumedCapacity    consumedCapacity = null;

            if (json.TryGetValue("ConsumedCapacity", out var consumedCapacityNode))
            {
                consumedCapacity = ConsumedCapacity.FromJson((JsonObject)consumedCapacityNode);
            }

            if (json.TryGetValue("Attributes", out var attributeNode))
            {
                attributes = AttributeCollection.FromJson((JsonObject)attributeNode);
            }

            return(new PutItemResult(attributes, consumedCapacity));
        }
Example #11
0
        public static TableRequests FromJson(string key, JsonArray batch)
        {
            var requests = new List <ItemRequest>();

            foreach (var r in batch)
            {
                var request = (JsonObject)r;

                /*
                 *              {
                 *                      ""PutRequest"": {
                 *                              ""Item"": {
                 *                                      ""name"": { ""S"": ""marcywilliams"" },
                 *                                      ""ownerId"": { ""N"": ""3033325"" },
                 *                                      ""type"": { ""N"": ""1"" }
                 *                              }
                 *                      },
                 *                      ""DeleteRequest"": {
                 *                              ""Key"": {
                 *                                      ""name"": { ""S"": ""marcywilliams"" },
                 *                                      ""ownerId"": { ""N"": ""3033325"" }
                 *                              }
                 *                      }
                 *              }
                 */

                if (request.TryGetValue("PutRequest", out var putRequestNode))
                {
                    var itemAttributes = AttributeCollection.FromJson((JsonObject)putRequestNode["Item"]);

                    requests.Add(new PutRequest(itemAttributes));
                }
                else if (request.TryGetValue("DeleteRequest", out var deleteRequestNode))
                {
                    var keyAttributes = AttributeCollection.FromJson((JsonObject)deleteRequestNode["Key"]);

                    requests.Add(new DeleteRequest(keyAttributes));
                }
            }

            return(new TableRequests(key, requests));
        }
Example #12
0
        public static QueryResult FromJson(JsonObject json)
        {
            ConsumedCapacity    consumedCapacity = null;
            AttributeCollection lastEvaluatedKey = null;

            if (json.TryGetValue("ConsumedCapacity", out var consumedCapacityNode))
            {
                consumedCapacity = ConsumedCapacity.FromJson((JsonObject)consumedCapacityNode);
            }

            if (json.TryGetValue("LastEvaluatedKey", out var lastEvaluatedKeyNode))
            {
                lastEvaluatedKey = AttributeCollection.FromJson((JsonObject)lastEvaluatedKeyNode);
            }

            AttributeCollection[] items;

            if (json.TryGetValue("Items", out var itemsNode))
            {
                var itemsJson = (JsonArray)itemsNode;

                items = new AttributeCollection[itemsJson.Count];

                for (var i = 0; i < items.Length; i++)
                {
                    items[i] = AttributeCollection.FromJson((JsonObject)itemsJson[i]);
                }
            }
            else
            {
                items = Array.Empty <AttributeCollection>();
            }

            return(new QueryResult(
                       consumedCapacity: consumedCapacity,
                       items: items,
                       lastEvaluatedKey: lastEvaluatedKey,
                       count: (int)json["Count"]
                       ));
        }
Example #13
0
        public async Task <IList <T> > ScanAsync(IEnumerable <KeyValuePair <string, object> > startKey = null,
                                                 Expression[] conditions = null,
                                                 int take = 1000)
        {
            var request = new ScanRequest(tableName)
            {
                Limit = take
            };

            if (conditions != null && conditions.Length > 0)
            {
                request.SetFilterExpression(DynamoExpression.Conjunction(conditions));
            }

            if (startKey != null)
            {
                request.ExclusiveStartKey = AttributeCollection.FromJson(startKey.ToJson());
            }

            var result = await client.Scan(request).ConfigureAwait(false);

            return(new QueryResult <T>(result));
        }
Example #14
0
        public static BatchGetItemResult FromJson(JsonObject json)
        {
            // var items = new List<AttributeCollection>();

            if (json.TryGetValue("ConsumedCapacity", out var consumedCapacityNode)) // Array
            {
                foreach (var item in (JsonArray)consumedCapacityNode)
                {
                    var unit = item.As <ConsumedCapacity>();

                    // TODO
                }
            }

            TableItemCollection[] responses;

            if (json.TryGetValue("Responses", out var responsesNode))
            {
                var tableElements = (JsonObject)responsesNode;

                var collections = new TableItemCollection[tableElements.Keys.Count];

                int i = 0;

                foreach (var tableEl in tableElements)
                {
                    var table = new TableItemCollection(tableEl.Key);

                    foreach (var item in (JsonArray)tableEl.Value)
                    {
                        table.Add(AttributeCollection.FromJson((JsonObject)item));
                    }

                    collections[i] = table;

                    i++;
                }

                responses = collections;
            }
            else
            {
                responses = Array.Empty <TableItemCollection>();
            }

            /*
             *          if (json.TryGetValue("UnprocessedKeys", out var unprocessedKeysNode))
             *          {
             *  var unprocessedKeys = new List<TableKeys>();
             *
             *                  foreach (var tableEl in (JsonObject)unprocessedKeysNode)
             *                  {
             *                          var tableName = tableEl.Key;
             *
             *                          foreach (var keyEl in (XArray) tableEl.Value["Keys"])
             *                          {
             *                                  var attributes = new KeyValuePair<string, object>();
             *
             *                                  foreach (var prop in (JsonObject)keyEl)
             *                                  {
             *                                          var name = prop.Key;
             *                                          var value = DbValue.FromJson((JsonObject)prop.Value);
             *                                  }
             *
             *                                  var key = new Key(attributes);
             *                          }
             *                  }
             *          }
             */

            return(new BatchGetItemResult(responses));
        }