Пример #1
0
        public static void RunDocumentModelSample(AmazonDynamoDBClient client)
        {
            Console.WriteLine("Loading hints table");
            Table table = Table.LoadTable(client, "packLevels");

            Console.WriteLine("Creating and saving first item");

            string wordbrain = @"D:\wordbrain.json";
            List<Pack> packs = JsonConvert.DeserializeObject<List<Pack>>(File.ReadAllText(wordbrain)) as List<Pack>;
            int packNum = 0;
            foreach (Pack p in packs)
            {
                packNum++;
                int level = 0;
                foreach (Answers item in p.levels)
                {
                    Document mydoc = new Document();
                    mydoc["puzzleID"] = p.pack + "_" + level;
                    DynamoDBList list = new DynamoDBList();
                    foreach (string str in item.answers)
                        list.Add(str);
                    mydoc.Add("answers", list);
                    table.PutItem(mydoc);
                    ++level;
                }

            }
        }
Пример #2
0
        // Returns a DynamoDB entry for the given JSON data
        private static DynamoDBEntry ToEntry(JsonData data, DynamoDBEntryConversion conversion)
        {
            if (data == null)
                return new DynamoDBNull();

            if (data.IsObject)
            {
                var document = new Document();
                foreach (var propertyName in data.PropertyNames)
                {
                    var nestedData = data[propertyName];
                    var entry = ToEntry(nestedData, conversion);
                    document[propertyName] = entry;
                }
                return document;
            }

            if (data.IsArray)
            {
                var list = new DynamoDBList();
                for(int i=0;i<data.Count;i++)
                {
                    var item = data[i];
                    var entry = ToEntry(item, conversion);
                    list.Add(entry);
                }
                return list;
            }

            if (data.IsBoolean)
                return new UnconvertedDynamoDBEntry((bool)data).Convert(conversion);

            if (data.IsDouble)
                return new UnconvertedDynamoDBEntry((double)data).Convert(conversion);

            if (data.IsInt)
                return new UnconvertedDynamoDBEntry((int)data).Convert(conversion);

            if (data.IsLong)
                return new UnconvertedDynamoDBEntry((long)data).Convert(conversion);

            if (data.IsString)
                return new UnconvertedDynamoDBEntry((string)data).Convert(conversion);

            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
                "Unable to convert JSON data of type {0} to DynamoDB type.", data.GetJsonType()));
        }
Пример #3
0
        // Attempts to decode a particular DynamoDBEntry.
        // May throw exceptions, in particular if the data is not base64 encoded
        private static bool TryDecodeBase64(DynamoDBEntry entry, out DynamoDBEntry decodedEntry)
        {
            decodedEntry = null;

            // Convert string primitive (S) to binary primitive (B)
            var primitive = entry as Primitive;
            if (primitive != null && primitive.Type == DynamoDBEntryType.String)
            {
                // Decode the contents
                var base64 = primitive.Value as string;
                byte[] bytes;
                if (!TryDecodeBase64(base64, out bytes))
                    return false;

                // Store as binary primitive (B)
                decodedEntry = new Primitive(bytes);
                return true;
            }

            // Convert string set (SS) to binary set (BS)
            var primitiveList = entry as PrimitiveList;
            if (primitiveList != null && primitiveList.Type == DynamoDBEntryType.String)
            {
                var decodedList = new PrimitiveList(DynamoDBEntryType.Binary);
                foreach(var item in primitiveList.Entries)
                {
                    // Attempt to decode
                    DynamoDBEntry decodedItem;
                    if (!TryDecodeBase64(item, out decodedItem))
                        return false;

                    // The decoded item must be a Primitive
                    Primitive decodedPrimitive = decodedItem as Primitive;
                    if (decodedPrimitive == null)
                        return false;

                    decodedList.Add(decodedPrimitive);
                }

                decodedEntry = decodedList;
                return true;
            }

            // In a given list (L), convert every string primitive (S) to binary primitive (B)
            // Non-strings and strings that cannot be converted will be left as-is
            var dynamoDBList = entry as DynamoDBList;
            if (dynamoDBList != null)
            {
                var decodedList = new DynamoDBList();
                foreach(var item in dynamoDBList.Entries)
                {
                    DynamoDBEntry decodedItem;
                    if (!TryDecodeBase64(item, out decodedItem))
                    {
                        // if decoding could not succeed, store same item
                        decodedItem = item;
                    }

                    decodedList.Add(decodedItem);
                }

                decodedEntry = decodedList;
                return true;
            }

            return false;
        }
Пример #4
0
        public static Document ToDocument <T>(T tObject, bool removeNulls = true, bool removeZeros = true)
        {
            var dictionary = tObject.GetType()
                             .GetProperties(BindingFlags.Instance | BindingFlags.Public)
                             .ToDictionary(prop => prop.Name, prop => prop.GetValue(tObject, null));

            if (removeNulls)
            {
                dictionary = dictionary.Where(x => x.Value != null).ToDictionary(y => y.Key, y => y.Value);
            }

            var document = new Document();

            foreach (var entry in dictionary)
            {
                if (entry.Value == null)
                {
                    continue;
                }

                var type = entry.Value.GetType();

                if (type == typeof(List <Object>))
                {
                    Console.WriteLine("WE HAVE A LIST"); // Look away. Nothing to see here. Please stop reading. Come on.... DO you feel better for reading this? Was it worth your time? Really have nothing better to do?                                                          COME ON. IT's Done. Nothing left. I promise. Not like there is any gold at the end of this comment. Please go on. We both have better things to be doing right now. I could be writting alot of usefull code, but instead we are both stuck reading this comment. Hope your happy now. I am not stopping before you are! Wonder if anyone is still reading. Not like anything usefull is coming later. Wonder how many skipped the first part. Please just go on with your day / night. THIS AINT OVER TO I SAY IT'S OVER!
                }

                if (type == typeof(string))
                {
                    document.Add(entry.Key, (string)entry.Value);
                }
                else if (type == typeof(int) || type == typeof(int?))
                {
                    if (removeZeros)
                    {
                        if ((int)entry.Value != 0)
                        {
                            document.Add(entry.Key, (int)entry.Value);
                        }
                    }
                    else
                    {
                        document.Add(entry.Key, (int)entry.Value);
                    }
                }
                else if (type == typeof(bool) || type == typeof(bool?))
                {
                    document.Add(entry.Key, new DynamoDBBool((bool)entry.Value));
                }
                else if (type == typeof(List <string>))
                {
                    document.Add(entry.Key, (List <string>)entry.Value);
                }
                else if (type == typeof(List <int>))
                {
                    var dynamoDBList = new DynamoDBList(new List <DynamoDBEntry>());

                    foreach (var i in (List <int>)entry.Value)
                    {
                        dynamoDBList.Add(i);
                    }
                    document.Add(entry.Key, dynamoDBList);
                }
                else if (type == typeof(DateTime))
                {
                    document.Add(entry.Key, (DateTime)entry.Value);
                }
                else if (type == typeof(float) || type == typeof(float?))
                {
                    if (removeZeros)
                    {
                        if ((float)entry.Value != 0)
                        {
                            document.Add(entry.Key, (float)entry.Value);
                        }
                    }
                    else
                    {
                        document.Add(entry.Key, (float)entry.Value);
                    }
                }
                else if (type == typeof(double) || type == typeof(double?))
                {
                    if (removeZeros)
                    {
                        if ((double)entry.Value != 0)
                        {
                            document.Add(entry.Key, (double)entry.Value);
                        }
                    }
                    else
                    {
                        document.Add(entry.Key, (double)entry.Value);
                    }
                }
                else if (type == typeof(uint?) || type == typeof(uint?))
                {
                    if (removeZeros)
                    {
                        if ((uint)entry.Value != 0)
                        {
                            document.Add(entry.Key, (uint)entry.Value);
                        }
                    }
                    else
                    {
                        document.Add(entry.Key, (uint)entry.Value);
                    }
                }
                else if (type.BaseType != null && type.BaseType.IsAssignableFrom(typeof(T)))
                {
                    if (type.BaseType == typeof(Object) && type.Name == "List`1") // If is list
                    {
                        var dynamoDbList = new DynamoDBList(new List <Document>());
                        var genericList  = entry.Value as IEnumerable;
                        if (genericList != null)
                        {
                            foreach (var test in genericList)
                            {
                                var dbEntry = test;
                                if (dbEntry != null)
                                {
                                    dynamoDbList.Add(AWSDocumentConverter.ToDocument(dbEntry, removeNulls));
                                }
                            }
                        }

                        document.Add(entry.Key, dynamoDbList);
                    }
                    else if (type.BaseType != null)
                    {
                        var docEntry = entry.Value;
                        if (docEntry != null)
                        {
                            document.Add(entry.Key, AWSDocumentConverter.ToDocument(docEntry, removeNulls));
                        }
                    }
                }
            }
            return(document);
        }
Пример #5
0
        private static void TestBinaryDecoding()
        {
            // Test data
            var data                = "Hello world!";
            var binaryData          = Encoding.UTF8.GetBytes(data);
            var base64Data          = Convert.ToBase64String(binaryData);
            var ms                  = new MemoryStream(binaryData);
            var binaryDataPrimitive = new Primitive(ms);

            var data2                = "Big River";
            var binaryData2          = Encoding.UTF8.GetBytes(data2);
            var base64Data2          = Convert.ToBase64String(binaryData2);
            var ms2                  = new MemoryStream(binaryData2);
            var binaryData2Primitive = new Primitive(binaryData2);

            var binarySet = new PrimitiveList(DynamoDBEntryType.Binary);

            binarySet.Add(binaryDataPrimitive);
            binarySet.Add(binaryData2Primitive);
            var binarySetContents = binarySet.AsListOfByteArray();

            var binaryList = new DynamoDBList();

            binaryList.Add(binaryDataPrimitive);
            binaryList.Add(binaryData2Primitive);
            var binaryListContents = binaryList.AsListOfByteArray();

            var base64Set = new PrimitiveList(DynamoDBEntryType.String);

            base64Set.Add(base64Data);
            base64Set.Add(base64Data2);
            var base64SetContents = new List <string> {
                base64Data, base64Data2
            };

            // Populate document
            var doc = new Document();

            doc["Binary"]       = binaryData;
            doc["BinarySet"]    = binarySet;
            doc["BinaryList"]   = binaryList;
            doc["BinaryString"] = base64Data;

            // Convert document to JSON
            var json       = doc.ToJson();
            var prettyJson = doc.ToJsonPretty();

            CompareJson(json, prettyJson);

            // Back to a document
            var rt = Document.FromJson(json);

            // Test to make sure binary data is strings
            var p = rt["Binary"] as Primitive;

            Assert.IsNotNull(p);
            Assert.AreEqual(DynamoDBEntryType.String, p.Type);

            p = rt["BinaryString"] as Primitive;
            Assert.IsNotNull(p);
            Assert.AreEqual(DynamoDBEntryType.String, p.Type);

            var s = rt["BinarySet"] as DynamoDBList;

            Assert.IsNotNull(s);
            Assert.AreEqual(2, s.Entries.Count);
            for (int i = 0; i < s.Entries.Count; i++)
            {
                var entry = s.Entries[i];
                Assert.IsTrue(entry is Primitive);
                Assert.AreEqual(DynamoDBEntryType.String, (entry as Primitive).Type);
                Assert.AreEqual(base64SetContents[i], entry.AsString());
            }

            var l = rt["BinaryList"] as DynamoDBList;

            Assert.IsNotNull(l);
            Assert.AreEqual(2, l.Entries.Count);
            for (int i = 0; i < l.Entries.Count; i++)
            {
                var entry = l.Entries[i];
                Assert.IsTrue(entry is Primitive);
                Assert.AreEqual(DynamoDBEntryType.String, (entry as Primitive).Type);
                Assert.AreEqual(base64SetContents[i], entry.AsString());
            }

            // Add a base64 set (SS, with base64 items)
            rt["BinarySet2"] = base64Set;

            // Decode all base64-encoded attributes
            rt.DecodeBase64Attributes("Binary", "BinarySet", "BinaryList", "BinarySet2", "BinaryString", "FakeAttribute");

            // Test to make sure attributes are binary
            var dp = rt["Binary"] as Primitive;

            Assert.IsNotNull(dp);
            Assert.AreEqual(DynamoDBEntryType.Binary, dp.Type);
            CollectionAssert.AreEqual(binaryData, dp.AsByteArray());

            dp = rt["BinaryString"] as Primitive;
            Assert.IsNotNull(dp);
            Assert.AreEqual(DynamoDBEntryType.Binary, dp.Type);
            CollectionAssert.AreEqual(binaryData, dp.AsByteArray());

            var dl = rt["BinaryList"] as DynamoDBList;

            Assert.IsNotNull(dl);
            Assert.AreEqual(2, dl.Entries.Count);
            for (int i = 0; i < dl.Entries.Count; i++)
            {
                var entry = dl.Entries[i];
                Assert.IsTrue(entry is Primitive);
                Assert.AreEqual(DynamoDBEntryType.Binary, (entry as Primitive).Type);
                CollectionAssert.AreEqual(entry.AsByteArray(), binarySetContents[i]);
            }

            dl = rt["BinarySet"] as DynamoDBList;
            Assert.IsNotNull(dl);
            Assert.AreEqual(2, dl.Entries.Count);
            for (int i = 0; i < dl.Entries.Count; i++)
            {
                var entry = dl.Entries[i];
                Assert.IsTrue(entry is Primitive);
                Assert.AreEqual(DynamoDBEntryType.Binary, (entry as Primitive).Type);
                CollectionAssert.AreEqual(entry.AsByteArray(), binarySetContents[i]);
            }

            var ds = rt["BinarySet2"] as PrimitiveList;

            Assert.IsNotNull(ds);
            Assert.AreEqual(2, ds.Entries.Count);
            Assert.AreEqual(DynamoDBEntryType.Binary, ds.Type);
            for (int i = 0; i < ds.Entries.Count; i++)
            {
                var entry = ds.Entries[i];
                Assert.IsTrue(entry is Primitive);
                Assert.AreEqual(DynamoDBEntryType.Binary, (entry as Primitive).Type);
                CollectionAssert.AreEqual(entry.AsByteArray(), binarySetContents[i]);
            }

            rt["FakeBinaryData"] = "this is fake";
            AssertExtensions.ExpectException(() => rt.DecodeBase64Attributes("FakeBinaryData"));
        }
Пример #6
0
        public override bool TryTo(object value, out DynamoDBList l)
        {
            var items = value as IEnumerable;
            if (items != null)
            {
                l = new DynamoDBList();
                foreach(var item in items)
                {
                    var itemType = item.GetType();
                    var entry = Conversion.ConvertToEntry(itemType, item);
                    l.Add(entry);
                }
                return true;
            }

            l = null;
            return false;
        }