예제 #1
0
        public static void SetUVTransform(this JContainer jsonNode, string desc, Vector4 data, int?digits = null)
        {
            var tx       = data.z;
            var ty       = data.w;
            var sx       = data.x;
            var sy       = data.y;
            var cx       = 0.0f;
            var cy       = 0.0f;
            var rotation = 0.0f;
            var c        = Math.Cos(rotation);
            var s        = Math.Sin(rotation);

            JArray arr = new JArray();

            arr.AddNumber(sx * c);
            arr.AddNumber(sx * s);
            arr.AddNumber(-sx * (c * cx + s * cy) + cx + tx);
            arr.AddNumber(-sy * s);
            arr.AddNumber(sy * c);
            arr.AddNumber(-sy * (-s * cx + c * cy) + cy + ty);
            arr.AddNumber(0.0);
            arr.AddNumber(0.0);
            arr.AddNumber(1.0);
            jsonNode.Add(new JProperty(desc, arr));
        }
예제 #2
0
        public static void WriteToJsonFile(AnimalCollection animalCollection, string path)
        {
            JContainer jsonFile = (JContainer)JObject.FromObject(new object());

            foreach (var animalType in animalCollection.GetTypes())
            {
                var animalArray = new JArray();
                foreach (var animal in animalCollection.List(animalType))
                {
                    var animalToken = JToken.FromObject(new
                    {
                        breed  = animal.Breed,
                        age    = animal.Age,
                        name   = animal.Name,
                        gender = animal.Gender.ToString()
                    });
                    animalArray.Add(animalToken);
                }
                var animalArrayWithLabel = new JProperty(animalType, animalArray);
                jsonFile.Add(animalArrayWithLabel);
            }

            var streamWriter = new StreamWriter(path);

            streamWriter.WriteLine(jsonFile);
            streamWriter.Flush();
            streamWriter.Close();
        }
예제 #3
0
        public static void SetTextureArray(this JContainer jsonNode, string key, Texture2DArrayData value)
        {
            var assetData = SerializeObject.SerializeAsset(value);
            var uri       = ExportSetting.instance.GetExportPath(assetData.uri);

            jsonNode.Add(new JProperty(key, uri));
        }
예제 #4
0
        static void MergeInto(JContainer left, JToken right)
        {
            foreach (var rightChild in right.Children <JProperty>())
            {
                var rightChildProperty = rightChild;
                var leftProperty       = left.SelectToken(rightChildProperty.Name);

                if (leftProperty == null)
                {
                    left.Add(rightChild);
                }
                else
                {
                    var leftObject = leftProperty as JObject;

                    if (leftObject == null)
                    {
                        var leftParent = (JProperty)leftProperty.Parent;
                        leftParent.Value = rightChildProperty.Value;
                    }
                    else
                    {
                        MergeInto(leftObject, rightChildProperty.Value);
                    }
                }
            }
        }
예제 #5
0
 public static void SetNumber(this JContainer jsonNode, string key, double value, float?defalutValue = null, int?digits = null)
 {
     if (value != defalutValue)
     {
         jsonNode.Add(new JProperty(key, Math.Round(value, digits ?? ExportSetting.instance.common.numberDigits)));
     }
 }
예제 #6
0
        public static void MergeInto(this JContainer left, JToken right)
        {
            foreach (var rightChild in right.Children <JProperty>())
            {
                var rightChildProperty = rightChild;
                var path = string.Empty.Equals(rightChildProperty.Name) || rightChildProperty.Name.Contains(" ")
                    ? $"['{rightChildProperty.Name}']"
                    : rightChildProperty.Name;

                var leftProperty = left.SelectToken(path);
                if (leftProperty == null)
                {
                    // no matching property, just add
                    left.Add(rightChild);
                }
                else
                {
                    var leftObject = leftProperty as JObject;
                    if (leftObject == null)
                    {
                        // replace value
                        var leftParent = (JProperty)leftProperty.Parent;
                        leftParent.Value = rightChildProperty.Value;
                    }
                    else
                    {
                        MergeInto(leftObject, rightChildProperty.Value);
                    }
                }
            }
        }
예제 #7
0
        public void SetRolePoints(ulong guildUserId, uint points)
        {
            JProperty[] properties = ((JContainer)_guildFile.ObjGuildFile["users"]).Values <JProperty>().ToArray();

            bool isUserInFile = false;

            foreach (JProperty property in properties)
            {
                if (property.Name == guildUserId.ToString())
                {
                    isUserInFile = true;
                    break;
                }
            }

            if (isUserInFile)
            {
                _guildFile.ObjGuildFile["users"][guildUserId.ToString()] = points.ToString();
            }
            else
            {
                JContainer container = (JContainer)_guildFile.ObjGuildFile["users"];
                container.Add(new JProperty(guildUserId.ToString(), points.ToString()));
                _guildFile.ObjGuildFile["users"] = container;
            }

            _guildFile.Save();
        }
예제 #8
0
        public static void MergeInto(this JContainer left, JToken right)
        {
            foreach (var rightChild in right.Children <JProperty>())
            {
                var rightChildProperty = rightChild;
                var leftProperty       = left.SelectToken(rightChildProperty.Name);

                if (leftProperty == null)
                {
                    // no matching property, just add
                    left.Add(rightChild);
                }
                else
                {
                    var leftObject = leftProperty as JObject;
                    if (leftObject == null)
                    {
                        // replace value
                        var leftParent = (JProperty)leftProperty.Parent;
                        leftParent.Value = rightChildProperty.Value;
                    }

                    else
                    {
                        // recurse object
                        MergeInto(leftObject, rightChildProperty.Value);
                    }
                }
            }
        }
        public static void MergeInto(this JContainer left, JToken right)
        {
            foreach (var rightChild in right.Children <JProperty>())
            {
                var rightChildProperty = rightChild;
                var leftProperty       = left.SelectToken(rightChildProperty.Name);

                if (leftProperty == null)
                {
                    left.Add(rightChild);
                }
                else
                {
                    var leftObject = leftProperty as JObject;
                    if (leftObject == null)
                    {
                        var leftParent = (JProperty)leftProperty.Parent;

                        if (leftParent.Value.GetType() == typeof(JArray))
                        {
                            leftParent.Value = JToken.FromObject(leftParent.Value.Concat(rightChildProperty.Value));
                        }
                        else
                        {
                            leftParent.Value = rightChildProperty.Value;
                        }
                    }

                    else
                    {
                        MergeInto(leftObject, rightChildProperty.Value);
                    }
                }
            }
        }
예제 #10
0
 public static void SetInt(this JContainer jsonNode, string key, int value, int?defalutValue = null)
 {
     if (value != defalutValue)
     {
         jsonNode.Add(new JProperty(key, value));
     }
 }
예제 #11
0
        /****************************************************************************/
        internal override void Evaluate(JContainer output, ExpressionContext context)
        {
            var newScope = _expression.Evaluate(context);
            var name     = _name.Evaluate(context).ToString();

            if (newScope is ExpandoObject expObject)
            {
                var json = expObject.ToJson();
                var data = JObject.Parse(json);

                output.Add(new JProperty(name, data));
            }
            else if (newScope is IEnumerable list)
            {
                output.Add(new JProperty(name, list.ToJArray()));
            }
        }
예제 #12
0
        /****************************************************************************/
        internal override void Evaluate(JContainer output, ExpressionContext context)
        {
            var array = JArray.Parse("[]");

            base.Evaluate(array, context);

            output.Add(new JProperty(this.Name.Evaluate(context).ToString(), array));
        }
예제 #13
0
        private static void AddObject(JContainer jsonList, object value)
        {
            if (jsonList == null)
            {
                throw new ArgumentNullException(nameof(jsonList));
            }

            jsonList.Add(value == null || value is JToken ? value : JToken.FromObject(value));
        }
예제 #14
0
        /// <summary>
        /// <para>Merge the right token into the left</para>
        /// </summary>
        /// <param name="left">Token to be merged into</param>
        /// <param name="right">Token to merge, overwriting the left</param>
        /// <param name="options">Options for merge</param>
        public static void MergeInto(
            this JContainer left, JToken right, MergeOptions options)
        {
            foreach (var rightChild in right.Children <JProperty>())
            {
                var rightChildProperty = rightChild;
                var leftPropertyValue  = left.SelectToken(rightChildProperty.Name);

                if (leftPropertyValue == null)
                {
                    // no matching property, just add
                    left.Add(rightChild);
                }
                else
                {
                    var leftProperty = (JProperty)leftPropertyValue.Parent;

                    var leftArray  = leftPropertyValue as JArray;
                    var rightArray = rightChildProperty.Value as JArray;
                    if (leftArray != null && rightArray != null)
                    {
                        switch (options.ArrayHandling)
                        {
                        case MergeOptionArrayHandling.Concat:

                            foreach (var rightValue in rightArray)
                            {
                                leftArray.Add(rightValue);
                            }

                            break;

                        case MergeOptionArrayHandling.Overwrite:

                            leftProperty.Value = rightChildProperty.Value;
                            break;
                        }
                    }

                    else
                    {
                        var leftObject = leftPropertyValue as JObject;
                        if (leftObject == null)
                        {
                            // replace value
                            leftProperty.Value = rightChildProperty.Value;
                        }

                        else
                        {
                            // recurse object
                            MergeInto(leftObject, rightChildProperty.Value, options);
                        }
                    }
                }
            }
        }
예제 #15
0
 public static void SetCubemap(this JContainer jsonNode, string key, Cubemap value, Cubemap defalutValue = null)
 {
     if (value != defalutValue)
     {
         var assetData = SerializeObject.SerializeAsset(value);
         var uri       = ExportSetting.instance.GetExportPath(assetData.uri);
         jsonNode.Add(new JProperty(key, uri));
     }
 }
예제 #16
0
        //-------------------------------扩展部分--------------------------------

        /**
         * 组件的公共部分,Vector2
         */
        public static void SetVector2(this JContainer jsonNode, string desc, Vector2 value, Vector2?defalutValue = null, int?digits = null)
        {
            if (defalutValue == null || (defalutValue != null && !value.Equals(defalutValue)))
            {
                JArray arr = new JArray();
                arr.AddNumber(value.x, digits);
                arr.AddNumber(value.y, digits);
                jsonNode.Add(new JProperty(desc, arr));
            }
        }
예제 #17
0
        /**
         * 组件的公共部分 Rect
         */
        public static void SetRect(this JContainer jsonNode, string desc, Rect data, int?digits = null)
        {
            JArray arr = new JArray();

            arr.AddNumber(data.x, digits);
            arr.AddNumber(data.y, digits);
            arr.AddNumber(data.width, digits);
            arr.AddNumber(data.height, digits);
            jsonNode.Add(new JProperty(desc, arr));
        }
예제 #18
0
 public static void SetColor3(this JContainer jsonNode, string desc, Color value, Color?defalutValue = null, int?digits = null)
 {
     if (defalutValue == null || (defalutValue != null && !value.Equals(defalutValue)))
     {
         JArray arr = new JArray();
         arr.AddNumber(value.r, digits);
         arr.AddNumber(value.g, digits);
         arr.AddNumber(value.b, digits);
         jsonNode.Add(new JProperty(desc, arr));
     }
 }
예제 #19
0
        public void WriteObjectInArrayStart()
        {
            ThrowIfReadOnly();

            _containers.Push(_currentContainer);

            var newContainer = new JObject();

            _currentContainer.Add(newContainer);
            _currentContainer = newContainer;
        }
예제 #20
0
        /****************************************************************************/
        internal override void Evaluate(JContainer output, ExpressionContext context)
        {
            var name = this.Name?.Evaluate(context)?.ToString() ?? "";
            var val  = this.Value.Evaluate(context);

            if (val is ExpandoObject)
            {
                val = val.ExpandoToObject <JObject>();
            }

            output.Add(new JProperty(name, val));
        }
예제 #21
0
        public JToken Build(JaBuilderContext context)
        {
            IEnumerable <IResource> elements = (IEnumerable <IResource>)context.Value;

            JContainer ja = elements.FirstOrDefault()?.GetContainer() ?? new JArray();

            foreach (var resource in elements)
            {
                ja.Add(resource.Serialize(context));
            }

            return(ja);
        }
예제 #22
0
        private JToken VisitInternal(JToken token, JContainer result)
        {
            foreach (var element in token)
            {
                switch (element)
                {
                case JProperty property:
                    var visitedProperty = VisitProperty(property);
                    result.Add(visitedProperty);
                    break;

                case JValue value:
                    result.Add(value);
                    break;

                default:
                    result.Add(Visit(element));
                    break;
                }
            }

            return(result);
        }
예제 #23
0
        public uint GetUserPoints(ulong userId)
        {
            JContainer container = (JContainer)_guildFile.ObjGuildFile["users"];

            if (container[userId.ToString()] != null)
            {
                return(uint.Parse((string)container[userId.ToString()]));
            }

            container.Add(new JProperty(userId.ToString(), 0));
            _guildFile.ObjGuildFile["users"] = container;
            _guildFile.Save();
            return(0);
        }
예제 #24
0
            private static void Add(JContainer container, QuestionNode node)
            {
                bool hasChildren = false;

                if (node.Children != null && node.Children.Count() > 0)
                {
                    hasChildren = true;
                    AddToContainer(container, node);
                }
                if (node.ArrayOfChildren != null && node.ArrayOfChildren.SelectMany(s => s).FirstOrDefault() != null)
                {
                    hasChildren = true;
                    //AddToJArray(response, node);
                }
                if (hasChildren == false)
                {
                    if (container is JObject)
                    {
                        var jproperty = (container as JObject).Descendants()
                                        .Where(t => t.Type == JTokenType.Property)
                                        .FirstOrDefault(s => ((JProperty)s).Name == node.NodeName);
                        if (jproperty != null)
                        {
                            (jproperty as JProperty).Value = node.Question.UserResponse.Value;
                        }
                        else
                        {
                            (container as JObject).Add(node.NodeName, node.Question.UserResponse.Value);
                        }
                    }
                    else if (container is JArray)
                    {
                        //Check if the container already has a JObject which can accept this Property
                        //TODO: Make this JContainer instead of JObject
                        JObject jObjectCandidate = (container as JArray).Children <JObject>()
                                                   .FirstOrDefault(o => o[node.NodeName] == null);//This checks if this JArray has a no JObject elements by this name.
                        //var jObjectCandidate = GetExistingChildContainerByName(container, node.NodeName);
                        if (jObjectCandidate == null)
                        {
                            jObjectCandidate = new JObject();
                            (jObjectCandidate as JObject).Add(node.NodeName, node.Question.UserResponse.Value);
                            container.Add(jObjectCandidate);
                        }
                        else if (jObjectCandidate is JObject)
                        {
                            (jObjectCandidate as JObject).Add(node.NodeName, node.Question.UserResponse.Value);
                        }
                    }
                }
            }
예제 #25
0
        public void SetRankPoints(ulong roleId, uint points)
        {
            if (_guildFile.ObjGuildFile["ranks"].Contains(roleId.ToString()))
            {
                _guildFile.ObjGuildFile["ranks"][roleId.ToString()] = points.ToString();
            }
            else
            {
                JContainer container = (JContainer)_guildFile.ObjGuildFile["ranks"];
                container.Add(new JProperty(roleId.ToString(), points.ToString()));
                _guildFile.ObjGuildFile["ranks"] = container;
            }

            _guildFile.Save();
        }
예제 #26
0
        private void AddObjectToLinksArray(JContainer data, object content)
        {
            JToken token = data[_propertyNamePrefix + LINKS_ARRAY];
            JArray links;

            if (token == null)
            {
                links = new JArray();
                data.Add(new JProperty(_propertyNamePrefix + LINKS_ARRAY, links));
            }
            else
            {
                links = (JArray)data[_propertyNamePrefix + LINKS_ARRAY];
            }
            links.Add(JToken.FromObject(content));
        }
예제 #27
0
        private void ModifyEnums(IList <JToken> enums)
        {
            foreach (var enumProperty in enums)
            {
                JContainer groupNode = enumProperty.Parent;

                bool isResponseType = enumProperty.Path.StartsWith("paths");

                string enumName;
                if (isResponseType)
                {
                    // man man man, als dit ooit netjes moet, terug naar rootnode en met jsonpath zoeken
                    JObject operationIdProperty = (JObject)enumProperty.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent;

                    string operationIdName = operationIdProperty.Value <string>("operationId");

                    bool hasMapping = EnumNameMapping.TryGetValue(operationIdName, out enumName);

                    if (!hasMapping)
                    {
                        enumName = operationIdName;
                    }
                }
                else
                {
                    string typeName = ((JProperty)groupNode.Parent).Name;
                    enumName = typeName.Substring(0, 1).ToUpper() + typeName.Substring(1);
                }


                try
                {
                    groupNode.Add(
                        new JProperty("x-ms-enum", new JObject
                    {
                        new JProperty("name", enumName),
                        new JProperty("modelAsString", false)
                    }));
                }
                catch (Exception)
                {
                    // ignore on error like 'already exists'
                }
            }
        }
예제 #28
0
        internal void AddValue(JValue value, JsonToken token)
        {
            if (_parent != null)
            {
                _parent.Add(value);
                _current = _parent.Last;

                if (_parent.Type == JTokenType.Property)
                {
                    _parent = _parent.Parent;
                }
            }
            else
            {
                _value   = value ?? JValue.CreateNull();
                _current = _value;
            }
        }
예제 #29
0
        private JContainer RemoveFields(JToken token, List <string> fields)
        {
            JContainer container = token as JContainer;

            if (container == null)
            {
                return(null);
            }

            List <JToken> removeList = new List <JToken>();

            List <JProperty> propertiesToAdd = new List <JProperty>();

            foreach (JToken el in container.Children())
            {
                JProperty p = el as JProperty;
                if (p != null && fields.Contains(p.Name))
                {
                    foreach (var item in p.Value.ToString().Split(','))
                    {
                        string[] keyValue      = item.Split(':');
                        string   propertySufix = Regex.Replace(keyValue[0], "[^0-9a-zA-Z]+", "");
                        string   propertyValue = Regex.Replace(keyValue[1], "[^0-9a-zA-Z]+", "");
                        propertiesToAdd.Add(new JProperty(string.Concat(p.Name, propertySufix), propertyValue));
                    }


                    removeList.Add(el);
                }
                RemoveFields(el, fields);
            }

            foreach (JToken el in removeList)
            {
                el.Remove();
            }

            foreach (JToken item in propertiesToAdd)
            {
                container.Add(item);
            }

            return(container);
        }
        public static void ExtendyBy(this JContainer left, JProperty right)
        {
            var leftProperty = left[right.Name];

            if (leftProperty == null)
            {
                left.Add(right);
            }
            else
            {
                var leftObject = leftProperty as JObject;
                if (leftObject == null)
                {
                    var leftParent = (JProperty)leftProperty.Parent;
                    leftParent.Value = right.Value;
                }
                else
                {
                    ExtendBy(leftObject, right.Value);
                }
            }
        }
예제 #31
0
        private static void MergeInto(JContainer left, JToken right)
        {
            foreach (var rightChild in right.Children<JProperty>())
            {
                var rightChildProperty = rightChild;
                var leftProperty = left.SelectToken(rightChildProperty.Name);

                if (leftProperty == null)
                {
                    left.Add(rightChild);
                }
                else
                {
                    var leftObject = leftProperty as JObject;

                    if (leftObject == null)
                    {
                        var leftParent = (JProperty) leftProperty.Parent;
                        leftParent.Value = rightChildProperty.Value;
                    }
                    else
                    {
                        MergeInto(leftObject, rightChildProperty.Value);
                    }
                }
            }
        }