예제 #1
0
 /// <summary>
 /// Creates an empty BSON document.
 /// <param name="useStringTable">Whether or not to generate and use a string table.</param>
 /// </summary>
 public BsonDocument(
     BsonStringTableMode mode = BsonStringTableMode.None,
     Dictionary <int, string> reverseStringTable = null)
 {
     _stringTableMode    = mode;
     _stringTable        = new Dictionary <string, int>();
     _reverseStringTable = reverseStringTable;
     Top = new BsonItem();
 }
예제 #2
0
 /// <summary>
 /// Creates an empty BSON document.
 /// <param name="useStringTable">Whether or not to generate and use a string table.</param>
 /// </summary>
 public BsonDocument(
     BsonStringTableMode mode = BsonStringTableMode.None, 
     Dictionary<int, string> reverseStringTable = null)
 {
     _stringTableMode = mode;
     _stringTable = new Dictionary<string, int>();
     _reverseStringTable = reverseStringTable;
     Top = new BsonItem();
 }
예제 #3
0
파일: Bson.cs 프로젝트: W-h-a-t-s/Rant
 public void BsonArray()
 {
     var document = new BsonDocument();
     document["test"] = new BsonItem(new string[] { "testing", "123" });
     var data = document.ToByteArray();
     var readDocument = BsonDocument.Read(data);
     Assert.AreEqual(2, readDocument["test"].Count);
     Assert.AreEqual("testing", readDocument["test"][0].Value);
     Assert.AreEqual("123", readDocument["test"][1].Value);
 }
예제 #4
0
파일: Bson.cs 프로젝트: W-h-a-t-s/Rant
 public void BsonArrayOfObjects()
 {
     var document = new BsonDocument();
     var item1 = new BsonItem();
     item1["prop"] = true;
     var item2 = new BsonItem();
     item2["pro2"] = false;
     document["test"] = new BsonItem(new BsonItem[] { item1, item2 });
     var data = document.ToByteArray();
     var readDocument = BsonDocument.Read(data);
     Assert.AreEqual(2, readDocument["test"].Count);
     Assert.AreEqual(true, readDocument["test"][0]["prop"].Value);
 }
예제 #5
0
파일: Bson.cs 프로젝트: W-h-a-t-s/Rant
 public void BsonComplexByteAccurate()
 {
     var document = new BsonDocument();
     document["number"] = 2;
     document["object"] = new BsonItem();
     document["object"]["array"] = new BsonItem(new BsonItem[]
     {
         "string",
         "another_string",
         2,
         5.2
     });
     document["object"]["float"] = 4.2223;
     document["str"] = "string";
     var data = document.ToByteArray();
     File.WriteAllBytes("ComplexTestOutput.bson", data);
     var exampleData = File.ReadAllBytes("ComplexTestFile.bson");
     CompareByteArrays(data, exampleData);
 }
예제 #6
0
        private void WriteItem(EasyWriter writer, BsonItem item, string name, bool isTop = false, bool isArray = false)
        {
            if(!isTop)
            {
                writer.Write(item.Type);
                writer.Write(isArray ? name : GetKeyName(name), Encoding.UTF8, true);
            }

            if (item.HasValues) // object or array - we need to write a document
            {
                var stream = new MemoryStream();
                var vWriter = new EasyWriter(stream);
                foreach(string key in item.Keys)
                    WriteItem(vWriter, item[key], key, false, item.IsArray);
                vWriter.Close();
                var data = stream.ToArray();
                // length of data + length of length + null terminator
                writer.Write(data.Length + 4 + 1);
                writer.Write(data);
                writer.Write((byte)0x00);
                return;
            }

            switch(item.Type)
            {
                case 0x01: // double
                    writer.Write((double)item.Value);
                    break;
                case 0x02: // string
                    var bytes = Encoding.UTF8.GetBytes(GetKeyName((string)item.Value, true));
                    writer.Write(bytes.Length + 1); // includes null terminator
                    writer.WriteBytes(bytes);
                    writer.Write((byte)0x00);
                    break;
                case 0x05: // binary
                    var byteArray = (byte[])item.Value;
                    writer.Write(byteArray.Length);
                    writer.Write((byte)0x00);
                    writer.Write(byteArray);
                    break;
                case 0x07: // objectid
                    writer.Write((byte[])item.Value);
                    break;
                case 0x08: // bool
                    writer.Write((bool)item.Value);
                    break;
                case 0x09: // UTC datetime
                case 0x11: // timestamp
                case 0x12: // 64 bit int
                    writer.Write((long)item.Value);
                    break;
                case 0x10: // 32 bit int
                    writer.Write((int)item.Value);
                    break;
                default:
                    throw new NotSupportedException($"Item type {item.Type} not supported.");
            }
        }
예제 #7
0
파일: Bson.cs 프로젝트: W-h-a-t-s/Rant
 public void BsonDeepObject()
 {
     var document = new BsonDocument();
     document["test"] = new BsonItem();
     document["test"]["test_level2"] = new BsonItem();
     document["test"]["test_level2"]["test_level3"] = true;
     var data = document.ToByteArray();
     var readDocument = BsonDocument.Read(data);
     Assert.AreEqual(true, readDocument["test"]["test_level2"]["test_level3"].Value);
 }
예제 #8
0
        private void WriteItem(EasyWriter writer, BsonItem item, string name, bool isTop = false, bool isArray = false)
        {
            if (!isTop)
            {
                writer.Write(item.Type);
                writer.Write(isArray ? name : GetKeyName(name), Encoding.UTF8, true);
            }

            if (item.HasValues) // object or array - we need to write a document
            {
                var stream  = new MemoryStream();
                var vWriter = new EasyWriter(stream);
                foreach (string key in item.Keys)
                {
                    WriteItem(vWriter, item[key], key, false, item.IsArray);
                }
                vWriter.Close();
                var data = stream.ToArray();
                // length of data + length of length + null terminator
                writer.Write(data.Length + 4 + 1);
                writer.Write(data);
                writer.Write((byte)0x00);
                return;
            }

            switch (item.Type)
            {
            case 0x01:     // double
                writer.Write((double)item.Value);
                break;

            case 0x02:                          // string
                var bytes = Encoding.UTF8.GetBytes(GetKeyName((string)item.Value, true));
                writer.Write(bytes.Length + 1); // includes null terminator
                writer.WriteBytes(bytes);
                writer.Write((byte)0x00);
                break;

            case 0x05:     // binary
                var byteArray = (byte[])item.Value;
                writer.Write(byteArray.Length);
                writer.Write((byte)0x00);
                writer.Write(byteArray);
                break;

            case 0x07:     // objectid
                writer.Write((byte[])item.Value);
                break;

            case 0x08:     // bool
                writer.Write((bool)item.Value);
                break;

            case 0x09:     // UTC datetime
            case 0x11:     // timestamp
            case 0x12:     // 64 bit int
                writer.Write((long)item.Value);
                break;

            case 0x10:     // 32 bit int
                writer.Write((int)item.Value);
                break;

            default:
                throw new NotSupportedException($"Item type {item.Type} not supported.");
            }
        }
예제 #9
0
        /// <summary>
        /// Saves the package to the specified file path.
        /// </summary>
        /// <param name="path">The path to the file to create.</param>
        public void Save(
            string path, 
            bool compress = true, 
            BsonStringTableMode stringTableMode = BsonStringTableMode.None)
        {
            if (String.IsNullOrEmpty(Path.GetExtension(path)))
            {
                path += ".rantpkg";
            }

            using (var writer = new EasyWriter(File.Create(path)))
            {
                var doc = new BsonDocument(stringTableMode);
                var info = doc.Top["info"] = new BsonItem();
                info["title"] = new BsonItem(_title);
                info["id"] = new BsonItem(_id);
                info["description"] = new BsonItem(Description);
                info["tags"] = new BsonItem(Tags);
                info["version"] = new BsonItem(Version.ToString());
                info["authors"] = new BsonItem(Authors);
				info["dependencies"] = new BsonItem(_dependencies.Select(dep =>
				{
					var depObj = new BsonItem();
					depObj["id"] = dep.ID;
					depObj["version"] = dep.Version.ToString();
					depObj["allow-newer"] = dep.AllowNewer;
					return depObj;
				}).ToArray());
                
                var patterns = doc.Top["patterns"] = new BsonItem();
                if(_patterns != null)
                    foreach(var pattern in _patterns)
                        patterns[pattern.Name] = new BsonItem(pattern.Code);

                var tables = doc.Top["tables"] = new BsonItem();
	            if (_tables != null)
	            {
					foreach (var table in _tables)
					{
						var t = tables[table.Name] = new BsonItem();
						t["name"] = new BsonItem(table.Name);
						t["subs"] = new BsonItem(table.Subtypes);
						t["language"] = new BsonItem(table.Language);
						t["hidden"] = new BsonItem(table.HiddenClasses.ToArray());
						t["hints"] = new BsonItem(0);
						var entries = new List<BsonItem>();
						foreach (var entry in table.GetEntries())
						{
							var e = new BsonItem();
							if (entry.Weight != 1)
								e["weight"] = new BsonItem(entry.Weight);
							var requiredClasses = entry.GetRequiredClasses().ToArray();
							if (requiredClasses.Length > 0)
								e["classes"] = new BsonItem(requiredClasses);
							var optionalClasses = entry.GetOptionalClasses().ToArray();
							if (optionalClasses.Length > 0)
								e["optional_classes"] = new BsonItem();
							var terms = new List<BsonItem>();
							foreach (var term in entry.Terms)
							{
								var et = new BsonItem();
								et["value"] = new BsonItem(term.Value);
								if (term.Pronunciation != "")
									et["pron"] = new BsonItem(term.Pronunciation);
								terms.Add(et);
							}
							e["terms"] = new BsonItem(terms.ToArray());
							entries.Add(e);
						}
						t["entries"] = new BsonItem(entries.ToArray());
					}
				}

                var data = doc.ToByteArray(stringTableMode != BsonStringTableMode.None);
                if (compress)
                    data = EasyCompressor.Compress(data);
                writer.Write(Encoding.ASCII.GetBytes("RANT"));
                writer.Write((uint)2);
                writer.Write(compress);
                writer.Write(data.Length);
                writer.Write(data);
            }
        }