public void TestEncodeToCBOR()
        {
            Cori       ciri1 = new Cori("coap://host");
            Cori       ciri2 = new Cori("http://host");
            CoralForm  form  = new CoralForm("http://apps.augustcellars.com/formRef", new Cori("coap://host/form1"));
            CBORObject cbor  = form.EncodeToCBORObject(ciri2, testDictionary);

            Assert.AreEqual(3, cbor[0].AsInt32());
            Assert.AreEqual(CBORType.Array, cbor[1].Type);
            Assert.AreEqual("[3, [0, \"http\", 1, \"apps.augustcellars.com\", 5, \"formRef\"], [0, \"coap\", 1, \"host\", 5, \"form1\"]]", cbor.ToString());

            form = new CoralForm("http://coreapps.org/base#lang", new Cori("coap://host/form1"));
            cbor = form.EncodeToCBORObject(ciri1, testDictionary);
            Assert.AreEqual("[3, 9, [5, \"form1\"]]", cbor.ToString());

            form = new CoralForm("http://coreapps.org/base#lang", new Cori("coap://host/form3"));
            cbor = form.EncodeToCBORObject(ciri1, testDictionary);
            Assert.AreEqual("[3, 9, 11]", cbor.ToString());

            form.FormFields.Add(new CoralFormField("http://apps.augustcellars.com/field1", CBORObject.True));
            form.FormFields.Add(new CoralFormField("http://apps.augustcellars.com/field2", CBORObject.FromObject(39)));
            form.FormFields.Add(new CoralFormField("http://apps.augustcellars.com/field3", new Cori("http://coreapps.org/base#update")));
            form.FormFields.Add(new CoralFormField("http://apps.augustcellars.com/field4", new Cori("coap://host3/form9")));
            form.FormFields.Add(new CoralFormField("http://apps.augustcellars.com/field6", new Cori("coap://host/form6")));
            form.FormFields.Add(new CoralFormField("http://coreapps.org/coap#type", new Cori("coap://host/form3")));

            cbor = form.EncodeToCBORObject(ciri1, testDictionary);
            Assert.AreEqual("[3, 9, 11, [[0, \"http\", 1, \"apps.augustcellars.com\", 5, \"field1\"], true, [0, \"http\", 1, \"apps.augustcellars.com\", 5, \"field2\"], 39, [0, \"http\", 1, \"apps.augustcellars.com\", 5, \"field3\"], 99999(4), [0, \"http\", 1, \"apps.augustcellars.com\", 5, \"field4\"], [1, \"host3\", 5, \"form9\"], [0, \"http\", 1, \"apps.augustcellars.com\", 5, \"field6\"], [5, \"form6\"], 8, 99999(11)]]", cbor.ToString());
        }
        public CoralBaseDirective(CBORObject value, Cori baseCori)
        {
            if (value.Type != CBORType.Array || value.Count != 2)
            {
                throw new ArgumentException();
            }

            if (value[0].Type != CBORType.Integer || value[0].IsTagged || value[0].AsInt32() != 1)
            {
                throw new ArgumentException();
            }

            if (value[1].Type != CBORType.Array)
            {
                throw new ArgumentException();
            }

            Cori temp = new Cori(value[1]);

            if (!temp.IsWellFormed())
            {
                throw new ArgumentException("base value is not well formed", nameof(value));
            }

            temp = temp.ResolveTo(baseCori);
            if (!temp.IsAbsolute())
            {
                throw new ArgumentException("new base URI must be an absolute URI", nameof(value));
            }

            BaseValue = temp;
        }
Beispiel #3
0
        public override CBORObject EncodeToCBORObject(Cori coriBase, CoralDictionary dictionary)
        {
            CBORObject node = CBORObject.NewArray();

            node.Add(3);
            node.Add(dictionary.Lookup(OperationType, false));

            CBORObject o = dictionary.Lookup(Target, false);

            if (o.Type == CBORType.Integer)
            {
                node.Add(o);
            }
            else
            {
                node.Add(Target.MakeRelative(coriBase).Data);
            }

            if (FormFields.Count > 0)
            {
                CBORObject fields = CBORObject.NewArray();
                foreach (CoralFormField f in FormFields)
                {
                    f.EncodeToCBOR(fields, coriBase, dictionary);
                }

                node.Add(fields);
            }

            return(node);
        }
Beispiel #4
0
        public static void SerializeResourceInCoral(IResource resource, CoralBody coral,
                                                    Dictionary <string, CBORObject> dictionary,
                                                    ResourceAttributes otherAttributes = null, Uri uriRelative = null,
                                                    bool isEndPoint = false)
        {
            CBORObject obj = CBORObject.NewArray();
            CBORObject href;

            if (uriRelative == null)
            {
                href = Cori.ToCbor(resource.Path + resource.Name);
            }
            else
            {
                href = Cori.ToCbor(new Uri(uriRelative, resource.Path + resource.Name));
            }

            CoralBody body = new CoralBody();

            SerializeAttributesInCoral(resource.Attributes, body, dictionary, uriRelative);
            if (otherAttributes != null)
            {
                SerializeAttributesInCoral(otherAttributes, body, dictionary, uriRelative);
            }

            if (body.Length == 0)
            {
                body = null;
            }

            CoralItem item = new CoralLink(isEndPoint ? "http://coreapps.org/ref#rd-unit" : "http://coreapps.org/reef#rd-item", href.AsString(), body);

            coral.Add(item);
        }
        public string EncodeToString(Cori contextCori, CoralUsing usingDictionary)
        {
            StringBuilder builder = new StringBuilder();

            BuildString(builder, "", contextCori, usingDictionary);

            return(builder.ToString());
        }
 /// <summary>
 /// Create a new CoRAL document based on a parsed CBOR object
 /// </summary>
 /// <param name="node">Encoded document to use</param>
 /// <param name="contextCori">Context to evaluate the document relative to</param>
 /// <param name="dictionary">Dictionary to be used to reverse compression</param>
 /// <returns></returns>
 public static CoralDocument DecodeFromCbor(CBORObject node, Cori contextCori, CoralDictionary dictionary = null)
 {
     if (dictionary == null)
     {
         dictionary = CoralDictionary.Default;
     }
     return(new CoralDocument(node, contextCori, dictionary));
 }
Beispiel #7
0
        public void TestConstructors()
        {
            Cori cori = new Cori("coap://host:99");

            Assert.ThrowsException <ArgumentException>(() => new CoralBody(CBORObject.DecodeFromBytes(Hex.Decode("01")), cori, null));
            Assert.ThrowsException <ArgumentException>(() => new CoralBody(CBORObject.DecodeFromBytes(Hex.Decode("830202820500")), cori, null));

            CoralBody body = new CoralBody(CBORObject.DecodeFromBytes(Hex.Decode("81830202820500")), cori, _testDictionary);

            Assert.AreEqual(1, body.Length);
            Assert.AreEqual("http://www.iana.org/assignments/relation/collection", ((CoralLink)body[0]).RelationTypeText);

            body = new CoralBody(CBORObject.DecodeFromBytes(Hex.Decode("828302028205008302006377766F")), cori, _testDictionary);
            Assert.AreEqual(2, body.Length);
            Assert.AreEqual("http://www.iana.org/assignments/relation/collection", ((CoralLink)body[0]).RelationTypeText);
            Assert.AreEqual("http://www.w3.org/1999/02/22-rdf-syntax-ns#type", ((CoralLink)body[1]).RelationTypeText);

            // [
            // [2
            // [0, "http", 1, "apps.augustcellars.com", 5, "rel1"],
            // [5, "target1"],
            // [
            // [2,
            // [0, "http", 1, "apps.augustcellars.com", 5, "rel2"],
            // [4, 1, 5, "target2"]], [1, [0, "coap", 1, "host3", 5, "link1"]], [2, [0, "http", 1, "apps.augustcellars.com", 5, "rel2"], [4, 1, 5, "target2"]]]], [2, [0, "http", 1, "apps.augustcellars.com", 5, "rel3"], [5, "target2"]], [1, [0, "http", 1, "host", 5, "link2", 5, "link3"]], [2, [0, "http", 1, "apps.augustcellars.com", 5, "rel4"], [4, 1, 5, "target2"]], [3, [0, "http", 1, "apps.augustcellars.com", 5, "op-type"], [4, 1, 5, "form"]]]

            body = new CoralBody(CBORObject.DecodeFromBytes(Hex.Decode("858402860064687474700176617070732E61756775737463656C6C6172732E636F6D056472656C3182056774617267657431838302860064687474700176617070732E61756775737463656C6C6172732E636F6D056472656C328404010567746172676574328201860064636F61700165686F73743305656C696E6B318302860064687474700176617070732E61756775737463656C6C6172732E636F6D056472656C328404010567746172676574328302860064687474700176617070732E61756775737463656C6C6172732E636F6D056472656C33820567746172676574328201880064687474700164686F737405656C696E6B3205656C696E6B338302860064687474700176617070732E61756775737463656C6C6172732E636F6D056472656C348404010567746172676574328303860064687474700176617070732E61756775737463656C6C6172732E636F6D05676F702D747970658404010564666F726D")), cori, _testDictionary);
            Assert.AreEqual(5, body.Length);

            Assert.IsTrue(body[0] is CoralLink);
            Assert.IsTrue(body[1] is CoralLink);
            Assert.IsTrue(body[2] is CoralBaseDirective);
            Assert.IsTrue(body[3] is CoralLink);
            Assert.IsTrue(body[4] is CoralForm);

            CoralLink link = (CoralLink)body[0];

            Assert.AreEqual("coap://host:99/target1", link.Target.ToString());
            Assert.AreEqual(3, link.Body.Length);

            Assert.IsTrue(link.Body[0] is CoralLink);
            Assert.IsTrue(link.Body[1] is CoralBaseDirective);
            Assert.IsTrue(link.Body[2] is CoralLink);

            CoralLink link2 = (CoralLink)link.Body[0];

            Assert.AreEqual("coap://host:99/target1/target2", link2.Target.ToString());
            link2 = (CoralLink)link.Body[2];
            Assert.AreEqual("coap://host3/link1/target2", link2.Target.ToString());

            link = (CoralLink)body[1];
            Assert.AreEqual("coap://host:99/target2", link.Target.ToString());

            link = (CoralLink)body[3];
            Assert.AreEqual("http://host/link2/link3/target2", link.Target.ToString());

            Assert.AreEqual("http://host/link2/link3/form", ((CoralForm)body[4]).Target.ToString());
        }
Beispiel #8
0
        public void Parse(string url, string cbor)
        {
            Cori o = new Cori(url);

            Assert.AreEqual(cbor, o.Data.ToString());
            string rebuild = o.ToString();

            Assert.AreEqual(url, rebuild);
        }
Beispiel #9
0
        public override CBORObject EncodeToCBORObject(Cori unused, CoralDictionary dictionary)
        {
            CBORObject node = CBORObject.NewArray();

            node.Add(1);
            node.Add(Cori.ToCbor(Uri));

            return(node);
        }
        /// <summary>
        /// Encode a CoRAL document to a binary value
        /// </summary>
        /// <param name="contextCori">Context to evaluate the document relative to</param>
        /// <param name="dictionary">Dictionary to be used for compression</param>
        /// <returns></returns>
        public byte[] EncodeToBytes(Cori contextCori, CoralDictionary dictionary = null)
        {
            if (dictionary == null)
            {
                dictionary = CoralDictionary.Default;
            }

            return(EncodeToCBORObject(contextCori, dictionary).EncodeToBytes());
        }
Beispiel #11
0
 public CoralForm(Cori formRef, Cori target)
 {
     if (!formRef.IsAbsolute())
     {
         throw new ArgumentException("operation must be absolute URI", nameof(formRef));
     }
     OperationType = formRef;
     Target        = target;
 }
        public CoralDictionary Add(int key, Cori value)
        {
            if (key < 0)
            {
                throw new ArgumentException("Key must be a non-negative value", nameof(key));
            }

            _dictionary.Add(key, value);
            return(this);
        }
Beispiel #13
0
        public CoralFormField(Cori fieldType, Cori value)
        {
            FieldType = fieldType;
            if (!value.IsAbsolute())
            {
                throw new ArgumentException("Must be an absolute CoRI value", nameof(value));
            }

            Url = value;
        }
        /// <summary>
        /// Reverse the dictionary encoding of a value.
        /// If it cannot be decoded then return null, in this case the value must be an integer.
        /// </summary>
        /// <param name="value">Value to decode</param>
        /// <param name="isIntLegal">Can an integer value be used here?</param>
        /// <returns>Original value if it can be decoded.</returns>
        public object Reverse(CBORObject value, bool isIntLegal)
        {
            if (value.IsTagged)
            {
                if (value.HasOneTag(DictionaryTag))
                {
                    return(Reverse(value.UntagOne(), false));
                }

                if (CoralItem.IsLiteral(value))
                {
                    return(value);
                }
                throw new ArgumentException("Value is not a literal value", nameof(value));
            }

            if (value.Type == CBORType.Integer && (isIntLegal || value.AsInt32() < 0))
            {
                return(value);
            }

            if (value.Type != CBORType.Integer)
            {
                if (value.Type == CBORType.Array || CoralItem.IsLiteral(value))
                {
                    return(value);
                }
                throw new ArgumentException($"The value '{value}' is not a literal value", nameof(value));
            }

            if (!_dictionary.ContainsKey(value.AsInt32()))
            {
                return(null);
            }


            object     o = _dictionary[value.AsInt32()];
            CBORObject result;

            if (o is Cori)
            {
                Cori cori = (Cori)o;
                result = cori.Data;
            }
            else if (o is CBORObject)
            {
                result = (CBORObject)o;
            }
            else
            {
                result = CBORObject.FromObject(o);
            }

            return(result);
        }
Beispiel #15
0
        /// <summary>
        /// Create a CoRAL link from the parameters
        /// </summary>
        /// <param name="relation">Cori value containing the relation - IRI</param>
        /// <param name="uriTarget">Absolute or relative CIRI for the target</param>
        /// <param name="body">attributes about the target</param>
        public CoralLink(Cori relation, Cori uriTarget, CoralBody body = null)
        {
            if (!relation.IsAbsolute())
            {
                throw new ArgumentException("Relation must be an absolute IRI", nameof(relation));
            }

            RelationType = relation;
            Target       = uriTarget;
            Body         = body;
        }
 /// <inheritdoc />
 public override void BuildString(StringBuilder builder, string pad, Cori contextCori, CoralUsing usingDictionary)
 {
     builder.Append(pad);
     if (contextCori != null)
     {
         builder.AppendFormat($"#base <{BaseValue.MakeRelative(contextCori)}>\n");
     }
     else
     {
         builder.AppendFormat($"#base <{BaseValue}>\n");
     }
 }
Beispiel #17
0
        public void Resolve(string baseUri, string href, string cbor)
        {
            Cori baseCori = new Cori(baseUri);
            Cori hrefCori = new Cori(href);

            Console.WriteLine(baseCori.Data.ToString());
            Console.WriteLine(hrefCori.Data.ToString());

            Cori resolved = hrefCori.ResolveTo(baseCori);

            Assert.AreEqual(cbor, resolved.Data.ToString());
        }
        public CoralBaseDirective(Cori value)
        {
            if (!value.IsWellFormed())
            {
                throw new ArgumentException("URI is not well formed", nameof(value));
            }

            if (!value.IsAbsolute())
            {
                throw new ArgumentException("URI is not absolute", nameof(value));
            }
            BaseValue = value;
        }
Beispiel #19
0
        // link = [2, relation-type, link-target, ?body]
        // relation-type = text
        // link-target = CoRI / literal
        // CoRI = <Defined in Section X of RFC XXXX>
        // literal = bool / int / float / time / bytes / text / null

        public override CBORObject EncodeToCBORObject(Cori coriBase, CoralDictionary dictionary)
        {
            CBORObject node = CBORObject.NewArray();

            node.Add(2);
            if (RelationType != null)
            {
                node.Add(dictionary.Lookup(RelationType, false));
            }
            else
            {
                node.Add(RelationTypeInt);
            }


            if (Target != null)
            {
                CBORObject o = dictionary.Lookup(Target, true);
                if (o.Type == CBORType.Integer)
                {
                    node.Add(o);
                }
                else
                {
                    if (coriBase != null)
                    {
                        Cori relative = Target.MakeRelative(coriBase);
                        node.Add(relative.Data);
                    }
                    else
                    {
                        node.Add(Target.Data);
                    }
                }
            }
            else if (Value != null)
            {
                node.Add(dictionary.Lookup(Value, true));
            }
            else if (TargetInt != null)
            {
                node.Add(CBORObject.FromObjectAndTag(TargetInt, CoralDictionary.DictionaryTag));
            }

            if (Body != null)
            {
                node.Add(Body.EncodeToCBORObject(coriBase, dictionary));
            }

            return(node);
        }
Beispiel #20
0
        public CoralFormField(CBORObject type, CBORObject value, Cori baseCori, CoralDictionary dictionary)
        {
            CBORObject o = (CBORObject)dictionary.Reverse(type, false);

            if (o == null)
            {
                FieldTypeInt = type.AsInt32();
            }
            else if (o.Type == CBORType.Array)
            {
                FieldType = new Cori(o);
                if (type.Type == CBORType.Integer)
                {
                    FieldTypeInt = type.AsInt32();
                }
            }
            else
            {
                throw new ArgumentException("Not a valid form field type");
            }

            o = (CBORObject)dictionary.Reverse(value, true);

            if (o == null)
            {
                LiteralInt = value.AsInt32();
            }
            else if (o.Type == CBORType.Array)
            {
                Url = new Cori(o);
                if (baseCori != null)
                {
                    Url = Url.ResolveTo(baseCori);
                }

                if (value.Type == CBORType.Integer)
                {
                    LiteralInt = value.Untag().AsInt32();
                }
            }
            else
            {
                Literal = o;
                if (value.IsTagged && value.HasOneTag(CoralDictionary.DictionaryTag) && value.Type == CBORType.Integer)
                {
                    LiteralInt = value.Untag().AsInt32();
                }
            }
        }
Beispiel #21
0
        /// <summary>
        /// Create a CoRAL link from the parameters
        /// </summary>
        /// <param name="relation">Cori version of the relation - IRI</param>
        /// <param name="target">Value of the target - Not a URI!!!</param>
        /// <param name="body">attributes about the target</param>
        public CoralLink(Cori relation, CBORObject target, CoralBody body = null)
        {
            if (!IsLiteral(target))
            {
                throw new ArgumentException("Value must be a literal value", nameof(target));
            }

            if (!relation.IsAbsolute())
            {
                throw new ArgumentException("Relation must be an absolute IRI", nameof(relation));
            }

            RelationType = relation;
            Value        = target;
            Body         = body;
        }
        /// <inheritdoc />
        public override CBORObject EncodeToCBORObject(Cori baseCori, CoralDictionary dictionary)
        {
            CBORObject result = CBORObject.NewArray();

            result.Add(1);
            if (baseCori != null)
            {
                Cori relative = BaseValue.MakeRelative(baseCori);
                result.Add(relative.Data);
            }
            else
            {
                result.Add(BaseValue.Data);
            }

            return(result);
        }
Beispiel #23
0
        public void BuildString(StringBuilder builder, string pad, Cori contextCori, CoralUsing usingDictionary)
        {
            Cori baseCori = contextCori;

            foreach (CoralItem item in _items)
            {
                if (item is CoralBaseDirective)
                {
                    baseCori = ((CoralBaseDirective)item).BaseValue;
                    item.BuildString(builder, pad, contextCori, usingDictionary);
                }
                else
                {
                    item.BuildString(builder, pad, baseCori, usingDictionary);
                }
            }
        }
Beispiel #24
0
        public void TestsFromKlaus(string line, string left, string middle, string right, string baseText, string relativeText, string resultText, string skip)
        {
            try {
                CBORObject cborLeft   = CBORObject.DecodeFromBytes(HexToString(left));
                CBORObject cborRight  = CBORObject.DecodeFromBytes(HexToString(right));
                CBORObject cborMiddle = CBORObject.DecodeFromBytes(HexToString(middle));

                // Console.WriteLine($"line # = {line}");
                // Console.WriteLine($"base = {cborLeft}");
                // Console.WriteLine($"href = {cborMiddle}");
                // Console.WriteLine($"result = {cborRight}");

                Cori coriBase   = new Cori(cborLeft);
                Cori coriRight  = new Cori(cborRight);
                Cori coriMiddle = new Cori(cborMiddle);

                Assert.IsTrue(coriBase.IsAbsolute());
                Assert.IsFalse(coriBase.IsRelative());
                Assert.IsTrue(coriRight.IsAbsolute());
                Assert.IsFalse(coriRight.IsRelative());
                Assert.IsTrue(coriMiddle.IsWellFormed());

                Cori result = coriMiddle.ResolveTo(coriBase);
                Assert.AreEqual(coriRight, result);

                if (cborMiddle.Count > 0 && cborMiddle[0].AsInt32() == 5 && cborMiddle[1].AsInt32() == 1)
                {
                    return;
                }

                Cori newHref = coriRight.MakeRelative(coriBase);
                // Console.WriteLine($"computed href = {newHref.Data}");

                Cori resolve2 = newHref.ResolveTo(coriBase);
                Assert.AreEqual(coriRight.Data.ToString(), resolve2.Data.ToString());

                if (skip != "0")
                {
                    // Assert.AreEqual(coriMiddle.Data.ToString(), newHref.Data.ToString());
                }
            }
            catch (Exception e) {
                Assert.Fail(e.ToString());
            }
        }
Beispiel #25
0
        public void DebugMe(int line, string left, string middle, string right, string baseText, string relativeText, string resultText, int skip)
        {
            CBORObject cborLeft   = CBORObject.DecodeFromBytes(HexToString(left));
            CBORObject cborRight  = CBORObject.DecodeFromBytes(HexToString(right));
            CBORObject cborMiddle = CBORObject.DecodeFromBytes(HexToString(middle));

            Console.WriteLine($"base = {cborLeft}");
            Console.WriteLine($"href = {cborMiddle}");
            Console.WriteLine($"result = {cborRight}");

            Cori coriBase   = new Cori(cborLeft);
            Cori ciriRight  = new Cori(cborRight);
            Cori ciriMiddle = new Cori(cborMiddle);

            Cori newHref = ciriRight.MakeRelative(coriBase);

            Assert.AreEqual(ciriMiddle.Data.ToString(), newHref.Data.ToString());
        }
Beispiel #26
0
        public override void BuildString(StringBuilder builder, string pad, Cori contextCori, CoralUsing usingDictionary)
        {
            builder.Append(pad);
            if (usingDictionary != null)
            {
                string t = usingDictionary.Abbreviate(RelationType.ToString());
                builder.Append(t);
            }
            else
            {
                builder.AppendFormat($"<{RelationType}>");
            }

            if (Target != null)
            {
                if (contextCori == null)
                {
                    builder.Append($" <{Target}>");
                }
                else
                {
                    builder.Append($" <{Target.MakeRelative(contextCori)}>");
                }
            }
            else if (Value != null)
            {
                builder.Append($" {Value}");
            }
            else if (TargetInt != null)
            {
                builder.Append($" ## {TargetInt} ##");
            }

            if (Body != null)
            {
                builder.Append(" [\n");
                Body.BuildString(builder, pad + "  ", contextCori, usingDictionary);
                builder.Append(pad);
                builder.Append("]");
            }

            builder.Append("\n");
        }
Beispiel #27
0
        /// <summary>
        /// Decode a CBOR object into a coral body.
        /// </summary>
        /// <param name="node">CBOR node to be decoded</param>
        /// <param name="contextCori">context to decode URIs</param>
        /// <param name="dictionary">Dictionary used for decompression</param>
        public CoralBody(CBORObject node, Cori contextCori, CoralDictionary dictionary)
        {
            Cori baseCori = contextCori;

            if (node.Type != CBORType.Array)
            {
                throw new ArgumentException("Invalid node type");
            }

            if (contextCori == null || !contextCori.IsAbsolute())
            {
                throw new ArgumentException("Must be resolved to an absolute URI", nameof(contextCori));
            }

            foreach (CBORObject child in node.Values)
            {
                if (child.Type != CBORType.Array)
                {
                    throw new ArgumentException("Invalid node type");
                }

                switch (child[0].AsInt32())
                {
                case 1:
                    CoralBaseDirective d1 = new CoralBaseDirective(child, contextCori);
                    _items.Add(d1);
                    baseCori = d1.BaseValue;
                    break;

                case 2:
                    _items.Add(new CoralLink(child, baseCori, dictionary));
                    break;

                case 3:
                    _items.Add(new CoralForm(child, baseCori, dictionary));
                    break;

                default:
                    throw new ArgumentException("Unrecognized CoRAL node type");
                }
            }
        }
Beispiel #28
0
        public CBORObject EncodeToCBORObject(Cori coriBase, CoralDictionary dictionary = null)
        {
            CBORObject root = CBORObject.NewArray();

            if (dictionary == null)
            {
                dictionary = CoralDictionary.Default;
            }

            foreach (CoralItem item in _items)
            {
                root.Add(item.EncodeToCBORObject(coriBase, dictionary));
                if (item is CoralBaseDirective d)
                {
                    coriBase = d.BaseValue.ResolveTo(coriBase);
                }
            }

            return(root);
        }
        public CBORObject Lookup(Cori value, bool isIntLegal)
        {
            if (!value.IsAbsolute())
            {
                return(value.Data);
            }

            foreach (KeyValuePair <int, object> o in _dictionary)
            {
                if (value.Equals(o.Value))
                {
                    if (isIntLegal)
                    {
                        return(CBORObject.FromObjectAndTag(o.Key, DictionaryTag));
                    }
                    return(CBORObject.FromObject(o.Key));
                }
            }

            return(value.Data);
        }
        public void AddKey()
        {
            CBORObject result = _dictionary.Lookup("missing", false);

            Assert.AreEqual("missing", result.AsString());

            _dictionary.Add(7, "missing");

            result = _dictionary.Lookup("missing", false);
            Assert.AreEqual(7, result.AsInt32());

            Cori cori = new Cori("coap://host2:99/path1/path2/path4");

            _dictionary.Add(8, cori);
            Cori ciri2 = new Cori(cori.ToString());

            result = _dictionary.Lookup(ciri2, false);
            Assert.AreEqual(8, result.AsInt32());
            _dictionary.Add(9, new Cori(CBORObject.DecodeFromBytes(Hex.Decode("8405000664666F726D"))));

            Assert.ThrowsException <ArgumentException>(() => _dictionary.Add(10, CBORObject.FromObject(DateTime.UtcNow)));
        }