예제 #1
0
        /// <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);
        }
예제 #2
0
        public CoralDictionary Add(int key, CBORObject value)
        {
            if (key < 0)
            {
                throw new ArgumentException("Key must be a non-negative value", nameof(key));
            }

            if (!CoralItem.IsLiteral(value))
            {
                throw new ArgumentException("Value must be a literal value");
            }

            if (value.Type == CBORType.TextString)
            {
                _dictionary.Add(key, value.AsString());
            }
            else
            {
                _dictionary.Add(key, value);
            }

            return(this);
        }
예제 #3
0
 public CoralBody Add(CoralItem item)
 {
     _items.Add(item);
     return(this);
 }