Пример #1
0
        public static int Compare(IComparable first, object second, SortOrder order)
        {
            FieldDataType firstType, secondType;
            TypeCode      firstActualType, secondActualType;

            firstType  = JSONType.GetJSONType(first, out firstActualType);
            secondType = JSONType.GetJSONType(second, out secondActualType);
            return(Compare(first, firstType, firstActualType, second, secondType, secondActualType, order));
        }
Пример #2
0
 public static void ForceSet <T>(IJSONDocument document, T value, IAttributeChain thisAttribute)
 {
     if (thisAttribute.Child == null)
     {
         if (thisAttribute.Indices != null)
         {
             var lengths = new int[thisAttribute.Indices.Length];
             for (int i = 0; i < thisAttribute.Indices.Length; i++)
             {
                 lengths[i] = thisAttribute.Indices[i] + 1;
             }
             Array array = Array.CreateInstance(typeof(object), lengths);
             array.SetValue(value, thisAttribute.Indices);
             document[thisAttribute.Name] = array;
         }
         else
         {
             document[thisAttribute.Name] = value;
         }
     }
     else
     {
         if (thisAttribute.Indices != null)
         {
             var lengths = new int[thisAttribute.Indices.Length];
             for (int i = 0; i < thisAttribute.Indices.Length; i++)
             {
                 lengths[i] = thisAttribute.Indices[i] + 1;
             }
             Array array = Array.CreateInstance(typeof(object), lengths);
             document[thisAttribute.Name] = array;
             var newDocument = JSONType.CreateNew();
             array.SetValue(newDocument, thisAttribute.Indices);
             ForceSet(newDocument, value, thisAttribute.Child);
         }
         else
         {
             if (!document.Contains(thisAttribute.Name) ||
                 document.GetAttributeDataType(thisAttribute.Name) != ExtendedJSONDataTypes.Object)
             {
                 document[thisAttribute.Name] = JSONType.CreateNew();
             }
             document = document.GetDocument(thisAttribute.Name);
             ForceSet(document, value, thisAttribute.Child);
         }
     }
 }
Пример #3
0
        private static void ExpandAttribute(IJSONDocument source, Queue <string> attributeQueue, object newValue)
        {
            string currentAttribute = attributeQueue.Dequeue();
            bool   lastAttribute    = attributeQueue.Count == 0;

            if (lastAttribute)
            {
                source[currentAttribute] = newValue;
            }
            else
            {
                if (source.Contains(currentAttribute))
                {
                    ExtendedJSONDataTypes type = source.GetAttributeDataType(currentAttribute);
                    switch (type)
                    {
                    case ExtendedJSONDataTypes.Object:
                        //Recurecurecurecurecurecurecurecurecurecurecurecurecurecurecursion
                        ExpandAttribute(source.GetDocument(currentAttribute), attributeQueue, newValue);
                        break;

                    default:
                        IJSONDocument subDocument = JSONType.CreateNew();
                        source[currentAttribute] = subDocument;
                        ExpandAttribute(subDocument, attributeQueue, newValue);
                        break;
                    }
                }
                else
                {
                    IJSONDocument subDocument = JSONType.CreateNew();
                    source[currentAttribute] = subDocument;
                    ExpandAttribute(subDocument, attributeQueue, newValue);
                }
            }
        }
Пример #4
0
        public static bool TryUpdate <T>(IJSONDocument document, T newValue, IAttributeChain thisAttribute, bool setNew)
        {
            bool isArray = false;

            if (document.Contains(thisAttribute.Name))
            {
                var type = document.GetAttributeDataType(thisAttribute.Name);
                if (thisAttribute.Child == null)
                {
                    if (thisAttribute.Indices != null)
                    {
                        if (type != ExtendedJSONDataTypes.Array)
                        {
                            return(false);
                        }
                        Array array;
                        if (document.TryGet(thisAttribute.Name, out array))
                        {
                            try
                            {
                                array.SetValue(newValue, thisAttribute.Indices);
                                document[thisAttribute.Name] = array;
                                return(true);
                            }
                            catch
                            {
                                return(false);
                            }
                        }
                        return(false);
                    }
                    document[thisAttribute.Name] = newValue;
                    return(true);
                }

                object fetch;

                if (thisAttribute.Indices != null)
                {
                    if (type != ExtendedJSONDataTypes.Array)
                    {
                        return(false);
                    }
                    Array array;
                    if (document.TryGet(thisAttribute.Name, out array))
                    {
                        try
                        {
                            fetch   = array.GetValue(thisAttribute.Indices);
                            isArray = true;
                        }
                        catch
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    fetch = document[thisAttribute.Name];
                }

                IJSONDocument newDocument = fetch as IJSONDocument;
                if (newDocument != null)
                {
                    bool result = TryUpdate(newDocument, newValue, thisAttribute.Child, setNew);
                    if (result)
                    {
                        if (isArray)
                        {
                            Array array;
                            if (document.TryGet(thisAttribute.Name, out array))
                            {
                                try
                                {
                                    array.SetValue(newDocument, thisAttribute.Indices);
                                    document[thisAttribute.Name] = array;
                                }
                                catch
                                {
                                    return(false);
                                }
                            }
                        }
                        else
                        {
                            document[thisAttribute.Name] = newDocument;
                        }
                    }
                    return(result);
                }

                var valueArray = fetch as Array;
                if (valueArray != null)
                {
                    bool result = TryUpdateInternalArray(valueArray, newValue, thisAttribute.Child, setNew);
                    if (result)
                    {
                        document[thisAttribute.Name] = valueArray;
                    }
                    return(result);
                }
                return(false);
            }
            if (setNew)
            {
                if (thisAttribute.Child == null)
                {
                    if (thisAttribute.Indices != null)
                    {
                        Array newArray = Array.CreateInstance(typeof(object),
                                                              thisAttribute.Indices.Select(index => index + 1).ToArray());
                        newArray.SetValue(newValue, thisAttribute.Indices);
                        document[thisAttribute.Name] = newArray;
                        return(true);
                    }
                    document[thisAttribute.Name] = newValue;
                    return(true);
                }
                object newChain = JSONType.CreateNew();
                if (thisAttribute.Indices != null)
                {
                    Array newArray = Array.CreateInstance(typeof(object),
                                                          thisAttribute.Indices.Select(index => index + 1).ToArray());
                    newArray.SetValue(newChain, thisAttribute.Indices);
                    newChain = newArray;
                }
                document[thisAttribute.Name] = newChain;

                return(TryUpdate(document, newValue, thisAttribute, setNew));
            }
            return(false);
        }