Пример #1
0
        /// <summary>
        /// Public constructor that accepts any object
        /// </summary>
        /// <param name="objValue">object to be reflected/evaluated for JSON conversion</param>
        public JSONReflector(object objValue)
        {
            Dictionary <JSONStringValue, JSONValue> jsonNameValuePairs = new Dictionary <JSONStringValue, JSONValue>();

            Type type = objValue.GetType();

            PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            foreach (PropertyInfo pi in properties)
            {
                if (pi.GetIndexParameters().Length == 0)
                {
                    JSONStringValue jsonParameterName  = new JSONStringValue(pi.Name);
                    JSONValue       jsonParameterValue = this.GetJSONValue(pi.GetValue(objValue, null));

                    if (jsonParameterValue != null)
                    {
                        jsonNameValuePairs.Add(jsonParameterName, jsonParameterValue);
                    }
                }
            }

            FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public);
            foreach (FieldInfo fi in fields)
            {
                JSONStringValue jsonParameterName  = new JSONStringValue(fi.Name);
                JSONValue       jsonParameterValue = this.GetJSONValue(fi.GetValue(objValue));

                if (jsonParameterValue != null)
                {
                    jsonNameValuePairs.Add(jsonParameterName, jsonParameterValue);
                }
            }

            this._jsonObjectCollection = new JSONObjectCollection(jsonNameValuePairs);
        }
Пример #2
0
        /// <summary>
        /// Serialize to a string in JSON format the snippets to replace in the page
        /// </summary>
        public override string ToString()
        {
            JSONObjectCollection ajaxResponse = new JSONObjectCollection();

            if (outer.Count > 0)
            {
                ajaxResponse.Add(outerValue, CreateCollection(outer));
            }
            if (list.Count > 0)
            {
                ajaxResponse.Add(listValue, CreateCollection(list));
            }
            if (hidden.Count > 0)
            {
                ajaxResponse.Add(hiddenValue, CreateCollection(hidden));
            }
            if (js.Count > 0)
            {
                ajaxResponse.Add(jsValue, Js);
            }
            if (blockJs.Count > 0)
            {
                ajaxResponse.Add(blockJsValue, blockJs);
            }

            foreach (KeyValuePair <JSONStringValue, JSONValue> kvp in this.otherValues)
            {
                ajaxResponse.Add(kvp.Key, kvp.Value);
            }

            return(ajaxResponse.ToString());
        }
Пример #3
0
        public bool reflashRoleCache()
        {
            bool isSuccess = false;

            ICriteria icr = CreateCriteria <RoleModel>();
            //icr.Add(Restrictions.Eq("isreserve", HotelModel.RESERVE_OK));
            IList                list     = icr.List();
            DatagridObject       datagrid = DatagridObject.ToDatagridObject(list);
            JSONObjectCollection json     = datagrid.toJSONObjectCollection();
            StringBuilder        sbr      = new StringBuilder("");

            sbr
            .Append("Class.forName(\"Toyz4js.cache\"); \n\n")
            .Append("Toyz4js[\"cache\"][\"RoleModel\"]=").Append(json.ToString()).Append(";  \n\n \n\n");
            ;
            try
            {
                FileUtil.WriteFile(this.pathRoleCache, sbr.ToString());
            }
            catch (Exception ex)
            {
                isSuccess = false;
            }
            return(isSuccess);
        }
Пример #4
0
        public bool reflashBrandCache()
        {
            bool isSucess = false;
            JSONObjectCollection jsonCraph = new JSONObjectCollection();

            StringBuilder sbr = new StringBuilder("");

            sbr
            .Append("Class.forName(\"Toyz4js.cache\"); \n\n");



            JSONObjectCollection jsonBrand = new JSONObjectCollection();

            IList <BrandModel> brands = CreateCriteria <BrandModel>().List <BrandModel>();

            sbr
            .Append("Toyz4js[\"cache\"][\"BrandModel\"][\"brands\"]=")
            .Append(DatagridObject.ToDatagridObject <BrandModel>(brands).toJSONObjectCollection().ToString())
            .Append("\n\n")
            ;

            isSucess = FileUtil.WriteFile(pathBrandCache, sbr.ToString());

            return(isSucess);
        }
Пример #5
0
        public JSONObjectCollection toJSONObjectCollection()
        {
            JSONObjectCollection json = new JSONObjectCollection();

            json.Add(new JSONStringValue("total"), new JSONNumberValue(this.total));
            json.Add(new JSONStringValue("rows"), JsonUtil.toJSONArrayCollection(this.rows));
            return(json);
        }
Пример #6
0
        public static JSONObjectCollection newCardDef(String sourceColumn, String languageCode, bool bVoice)
        {
            JSONObjectCollection ret = new JSONObjectCollection();

            ret.Add(new JSONStringValue(COLUMNSOURCE), new JSONStringValue(sourceColumn));
            ret.Add(new JSONStringValue(LANGUAGECODE), new JSONStringValue(languageCode));
            ret.Add(new JSONStringValue(TYPE), new JSONBoolValue(bVoice));

            return(ret);
        }
Пример #7
0
        public void AddToOuter(String name, string value)
        {
            string tagName;
            string innerHTML;
            JSONObjectCollection attributes;

            SplitHTMLChunk(value.Trim(), out tagName, out innerHTML, out attributes);

            JSONObjectCollection splitOuter = new JSONObjectCollection();

            splitOuter.Add(innerValue, new JSONStringValue(innerHTML, true));
            splitOuter.Add(attributesValue, attributes);

            add(outer, new JSONStringValue(name), splitOuter);
        }
Пример #8
0
        private static JSONObjectCollection CreateCollection(Dictionary <JSONStringValue, JSONValue> keyValues)
        {
            // Why this instead of passing the dictionary directly to the JSONObjectCollection constructor.
            // Because of Java that's why.
            // We translate JSONObjectCollection to json.org java JSONObject.
            // When we upgraded the json.org library generics where added to the constructor that accepted a map.
            // The generics used are incompatible with our maps and the constructor that accepts any object doesn't
            // do what we want. So we ended up adding one entry at a time.
            var c = new JSONObjectCollection();

            foreach (var keyValue in keyValues)
            {
                c.Add(keyValue.Key, keyValue.Value);
            }
            return(c);
        }
Пример #9
0
        public bool reflashDictCache()
        {
            bool isSuccess = false;



            JSONObjectCollection jsonGraph = new JSONObjectCollection();

            JSONObjectCollection jarr = new JSONObjectCollection();

            ICriteria             icr       = CreateCriteria <DictTypeModel>();
            IList <DictTypeModel> dictTypes = icr.List <DictTypeModel>();

            icr = CreateCriteria <DictModel>();
            IList <DictModel> dicts = icr.List <DictModel>();

            foreach (DictTypeModel dictType in dictTypes)
            {
                IList <DictModel> tempDicts = new List <DictModel>();
                foreach (DictModel dict in dicts)
                {
                    if (dict.type != dictType.id)
                    {
                        continue;
                    }
                    tempDicts.Add(dict);
                }
                jsonGraph.Add(new JSONStringValue(dictType.id), DatagridObject.ToDatagridObject <DictModel>(tempDicts).toJSONObjectCollection());
            }

            StringBuilder sbr = new StringBuilder("");

            sbr
            .Append("Class.forName(\"Toyz4js.cache\"); \n\n")
            .Append("Toyz4js[\"cache\"][\"DictModel\"]=").Append(jsonGraph.ToString()).Append(";  \n\n \n\n")
            .Append("Toyz4js[\"cache\"][\"DictTypeModel\"]=").Append(DatagridObject.ToDatagridObject <DictTypeModel>(dictTypes).toJSONObjectCollection()).Append(";  \n\n \n\n")
            ;
            try
            {
                FileUtil.WriteFile(this.pathDictCache, sbr.ToString());
            }
            catch (Exception ex) {
                isSuccess = false;
            }
            return(isSuccess);
        }
Пример #10
0
        public string GetValue()
        {
            var parsedJsonSource = GetVariables()[jsonName];

            var parsedJson = parser.Parse(parsedJsonSource) as JSONObjectCollection;

            double
                val1 = ((JSONNumber)parsedJson[val1Name]).Value,
                val2 = ((JSONNumber)parsedJson[val2Name]).Value;

            string   operatStr = ((JSONString)parsedJson[operatName]).Value;
            Operator operat;

            switch (operatStr)
            {
            case "-":
                operat = Operator.minus;
                break;

            case "+":
                operat = Operator.plus;
                break;

            case "*":
                operat = Operator.multiple;
                break;

            case "/":
                operat = Operator.divide;
                break;

            default:

                throw new FormatException();
            }

            var result = CalcThis(val1, val2, operat);

            var resultJson = new JSONObjectCollection
            {
                new JSONNumber(resultName, result)
            };

            return(resultJson.ToString());
        }
Пример #11
0
        private void SplitHTMLChunk(string value, out string tagName, out string innerHTML, out JSONObjectCollection attributes)
        {
            Debug.Assert(value.StartsWith("<"), "HTML chunk doesn't start with an HTML tag");
            Debug.Assert(value.EndsWith(">"), "HTML chunk doesn't end with an HTML tag");

            string tagContents = tagContentsRE.Match(value).Value;

            tagName = tagRE.Match(tagContents).Groups[1].Value;

            int innerStart     = tagContents.Length;
            int lastClosingTag = value.LastIndexOf('<');
            int innerLength    = lastClosingTag - innerStart;


            // Custom widgets can have initialization scripts appended in the end
            if (tagName.ToLower() != "script" && value.Substring(lastClosingTag).ToLower() == "</script>")
            {
                var lastScriptStart = value.LastIndexOf("<script", StringComparison.OrdinalIgnoreCase);
                var initScript      = value.Substring(lastScriptStart);
                innerHTML  = (innerLength > 0 ? value.Substring(innerStart, lastScriptStart - innerStart) : "");
                innerHTML += initScript;
            }
            else
            {
                innerHTML = (innerLength > 0 ? value.Substring(innerStart, innerLength) : "");
            }

            MatchCollection matches = attributeRE.Matches(tagContents);

            attributes = new JSONObjectCollection();
            foreach (Match m in matches)
            {
                string attrName  = m.Groups[1].Value;
                string attrValue = m.Groups[2].Value;
                attrValue = attrValue.Substring(1, attrValue.Length - 2); // Remove the single or double quotes
                attributes.Add(new JSONStringValue(attrName), new JSONStringValue(attrValue));
            }
        }
Пример #12
0
        public static JSONValue GetJsonObjcet(object obj)
        {
            if (obj == null)
            {
                return(new JSONObjectCollection());
            }
            Type      type      = obj.GetType();
            JSONValue jsonValue = new JSONObjectCollection();

            if (ObjectUtil.IsDictionary <string, object>(obj))
            {
                /*
                 * Dictionary<string, object> dict = obj as Dictionary<string, object>;
                 * JSONObjectCollection jsonObject = new JSONObjectCollection();
                 * foreach (string key in dict.Keys)
                 * {
                 *  object temp = dict[key];
                 *  if (temp == null) continue;
                 *  if (ObjectUtil.IsList(obj))
                 *  {
                 *      jsonObject.Add(new JSONStringValue(key), GetJsonList(temp as IList));
                 *  }
                 *  else
                 *  {
                 *      jsonObject.Add(new JSONStringValue(key), GetJsonObjcet(temp));
                 *  }
                 * }
                 * jsonValue = jsonObject;
                 */
            }
            else
            {
                JSONReflector jsonRef = new JSONReflector(obj);
                jsonValue = jsonRef;
            }
            return(jsonValue);
        }
Пример #13
0
        private JSONValue GetJSONValue(object objValue)
        {
            if (objValue != null)
            {
                Type      thisType  = objValue.GetType();
                JSONValue jsonValue = null;

                if (thisType == typeof(System.Int32))
                {
                    jsonValue = new JSONNumberValue(Convert.ToInt32(objValue));
                }
                else if (thisType == typeof(System.Int16))
                {
                    jsonValue = new JSONNumberValue(Convert.ToInt16(objValue));
                }
                else if (thisType == typeof(System.Int64))
                {
                    jsonValue = new JSONNumberValue(Convert.ToInt64(objValue));
                }
                else if (thisType == typeof(System.Single))
                {
                    jsonValue = new JSONNumberValue(Convert.ToSingle(objValue));
                }
                else if (thisType == typeof(System.DateTime))
                {
                    jsonValue = new JSONStringValue(Convert.ToDateTime(objValue).ToString("yyyy-MM-dd HH:mm"));
                }
                else if (thisType == typeof(System.Double))
                {
                    jsonValue = new JSONNumberValue(Convert.ToDouble(objValue));
                }
                else if (thisType == typeof(System.Decimal))
                {
                    jsonValue = new JSONNumberValue(Convert.ToDecimal(objValue));
                }
                else if (thisType == typeof(System.Byte))
                {
                    jsonValue = new JSONNumberValue(Convert.ToByte(objValue));
                }
                else if (thisType == typeof(System.String))
                {
                    jsonValue = new JSONStringValue(Convert.ToString(objValue));
                }
                else if (thisType == typeof(System.Boolean))
                {
                    jsonValue = new JSONBoolValue(Convert.ToBoolean(objValue));
                }
                else if (thisType.BaseType == typeof(System.Enum))
                {
                    jsonValue = new JSONStringValue(Enum.GetName(thisType, objValue));
                }
                else if (thisType.IsArray)
                {
                    List <JSONValue> jsonValues = new List <JSONValue>();
                    Array            arrValue   = (Array)objValue;
                    for (int x = 0; x < arrValue.Length; x++)
                    {
                        JSONValue jsValue = this.GetJSONValue(arrValue.GetValue(x));
                        jsonValues.Add(jsValue);
                    }
                    jsonValue = new JSONArrayCollection(jsonValues);
                }
                else if (objValue is IList)
                {
                    List <JSONValue> jsonValues = new List <JSONValue>();
                    IList            list       = (IList)objValue;

                    JSONArrayCollection tColl = new JSONArrayCollection();
                    foreach (object value in list)
                    {
                        Dictionary <JSONStringValue, JSONValue> objs = new Dictionary <JSONStringValue, JSONValue>();
                        Type vType = value.GetType();

                        if (vType == typeof(System.String))
                        {
                            tColl.Add(new JSONStringValue(Convert.ToString(value)));
                            continue;
                        }

                        PropertyInfo[] pis = vType.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
                        foreach (PropertyInfo p in pis)
                        {
                            JSONStringValue jsonParameterName  = new JSONStringValue(p.Name);
                            JSONValue       jsonParameterValue = this.GetJSONValue(p.GetValue(value, null));
                            if (jsonParameterValue != null)
                            {
                                if (jsonParameterValue != null)
                                {
                                    objs.Add(jsonParameterName, jsonParameterValue);
                                }
                            }
                        }
                        tColl.Add(new JSONObjectCollection(objs));
                    }
                    //jsonValue = new JSONObjectCollection(objs);
                    return(tColl);
                }

                else if (objValue.GetType().IsClass)
                {
                    Dictionary <JSONStringValue, JSONValue> jsonNameValuePairs = new Dictionary <JSONStringValue, JSONValue>();
                    PropertyInfo[] properties = thisType.GetProperties();
                    foreach (PropertyInfo pi in properties)
                    {
                        JSONStringValue jsonParameterName  = new JSONStringValue(pi.Name);
                        JSONValue       jsonParameterValue = this.GetJSONValue(pi.GetValue(objValue, null));


                        if (jsonParameterValue != null)
                        {
                            if (jsonParameterValue != null)
                            {
                                jsonNameValuePairs.Add(jsonParameterName, jsonParameterValue);
                            }
                        }
                    }
                    JSONObjectCollection resJson = new JSONObjectCollection(jsonNameValuePairs);
                    return(resJson);
                }
                return(jsonValue);
            }
            return(null);
        }
Пример #14
0
        /// <summary>
        /// An example implementation of JSONSharp. The location of a purveyor of fine aleas is used
        /// to show how different data types are handled when converted to JSON format at render time.
        /// </summary>
        /// <param name="args">command-line arguments; none used in this example</param>
        public static void Main(string[] args)
        {
            // Create a Dictionary of name/value pairs for the entire JSON object
            Dictionary <JSONStringValue, JSONValue> jsonNameValuePairs = new Dictionary <JSONStringValue, JSONValue>();

            // Create and add name/value pairs for business, address, city, state,
            // zipcode, latitude, longitude, covercharge and url
            JSONStringValue jsv_name_Business  = new JSONStringValue("business");
            JSONStringValue jsv_value_Business = new JSONStringValue("Tractor Tavern");

            jsonNameValuePairs.Add(jsv_name_Business, jsv_value_Business);

            JSONStringValue jsv_name_Address  = new JSONStringValue("address");
            JSONStringValue jsv_value_Address = new JSONStringValue("5213 Ballard Ave NW");

            jsonNameValuePairs.Add(jsv_name_Address, jsv_value_Address);

            JSONStringValue jsv_name_City  = new JSONStringValue("city");
            JSONStringValue jsv_value_City = new JSONStringValue("Seattle");

            jsonNameValuePairs.Add(jsv_name_City, jsv_value_City);

            JSONStringValue jsv_name_State  = new JSONStringValue("state");
            JSONStringValue jsv_value_State = new JSONStringValue("WA");

            jsonNameValuePairs.Add(jsv_name_State, jsv_value_State);

            JSONStringValue jsv_name_Zipcode  = new JSONStringValue("zipcode");
            JSONNumberValue jsv_value_Zipcode = new JSONNumberValue(98107);

            jsonNameValuePairs.Add(jsv_name_Zipcode, jsv_value_Zipcode);

            JSONStringValue jsv_name_Latitude  = new JSONStringValue("latitude");
            JSONNumberValue jsv_value_Latitude = new JSONNumberValue(47.665663);

            jsonNameValuePairs.Add(jsv_name_Latitude, jsv_value_Latitude);

            JSONStringValue jsv_name_Longitude  = new JSONStringValue("longitude");
            JSONNumberValue jsv_value_Longitude = new JSONNumberValue(-122.382343);

            jsonNameValuePairs.Add(jsv_name_Longitude, jsv_value_Longitude);

            JSONStringValue jsv_name_CoverCharge  = new JSONStringValue("covercharge");
            JSONBoolValue   jsv_value_CoverCharge = new JSONBoolValue(true);

            jsonNameValuePairs.Add(jsv_name_CoverCharge, jsv_value_CoverCharge);

            JSONStringValue jsv_name_Url  = new JSONStringValue("url");
            JSONStringValue jsv_value_Url = new JSONStringValue("http://tractortavern.citysearch.com/");

            jsonNameValuePairs.Add(jsv_name_Url, jsv_value_Url);


            // Add an array of payment methods
            JSONStringValue  jsv_name_PaymentMethods = new JSONStringValue("paymentmethods");
            List <JSONValue> listPaymentMethods      = new List <JSONValue>();

            listPaymentMethods.Add(new JSONStringValue("Cash"));
            listPaymentMethods.Add(new JSONStringValue("Visa"));
            listPaymentMethods.Add(new JSONStringValue("Mastercard"));
            listPaymentMethods.Add(new JSONStringValue("American Express"));
            JSONArrayCollection jsv_value_PaymentMethods = new JSONArrayCollection(listPaymentMethods);

            jsonNameValuePairs.Add(jsv_name_PaymentMethods, jsv_value_PaymentMethods);

            // Construct our object, passing the Dictionary of name/value pairs.  We could have
            // created our JSONObjectCollection first, then called the Add() method to populate
            // the object's internal Dictionary.
            JSONObjectCollection jsonObjectCollection = new JSONObjectCollection(jsonNameValuePairs);

            // The ToString() is the compact representation of the object's JSON output
            Console.WriteLine("JSONObjectCollection.ToString()");
            Console.WriteLine("===============================");
            Console.WriteLine(jsonObjectCollection.ToString());
            Console.WriteLine("===============================");
            Console.WriteLine();
            // PrettyPrint() is great for readability
            Console.WriteLine("JSONObjectCollection.PrettyPrint()");
            Console.WriteLine("===============================");
            Console.WriteLine(jsonObjectCollection.PrettyPrint());
            Console.WriteLine("===============================");
        }