Exemple #1
0
        public static SimpleJSONNode MoBackTypedObjectToJSON(object data, MoBackValueType dataType)
        {
            switch (dataType)
            {
            case MoBackValueType.String:
                return(data == null ? "" : (string)data);

            case MoBackValueType.Boolean:
                return((bool)data);

            case MoBackValueType.Number:
                MoBackNumber number = (MoBackInternal.MoBackNumber)data;
                return(number.GetJSON());

            case MoBackValueType.Date:
                return(MoBackDate.DateToMobackFormatJSON((DateTime)data));

            case MoBackValueType.File:
                return(((MoBackFile)data).GetJSON());

            case MoBackValueType.Pointer:
                return(MoBackPointer.PointerToMoBackJSON((MoBackPointer)data));

            case MoBackValueType.Relation:
                return(MoBackRelation.MoBackJSONFromRelation((MoBackRelation)data));

            case MoBackValueType.MoBackObject:
                if (data != null)
                {
                    return(((MoBackObject)data).GetJSON());
                }
                else
                {
                    return(new SimpleJSONClass());
                }

            case MoBackValueType.Array:
                return(((MoBackArray)data).GetJSON());

            default:
                throw new NotImplementedException();
            }
        }
Exemple #2
0
        public static object MoBackTypedObjectFromJSON(SimpleJSONNode node, out MoBackValueType dataType)
        {
            if (MoBackAppSettings.doublePrecisionFloatingPoint && node.valueType == typeof(double))
            {
                dataType = MoBackValueType.Number;
                return(new MoBackNumber(node.AsDouble));
            }
            else if (!MoBackAppSettings.doublePrecisionFloatingPoint && node.valueType == typeof(float))
            {
                dataType = MoBackValueType.Number;
                return(new MoBackNumber(node.AsFloat));
            }
            else if (node.valueType == typeof(int))
            {
                dataType = MoBackValueType.Number;
                return(new MoBackNumber(node.AsInt));
            }
            else if (node.valueType == typeof(bool))
            {
                dataType = MoBackValueType.Boolean;
                return(node.AsBool);
            }
            else if (node.valueType == typeof(string))
            {
                dataType = MoBackValueType.String;
                return(node.Value);
            }
            else if (node.valueType == typeof(SimpleJSONClass))
            {
                SimpleJSONClass nestedClass = (SimpleJSONClass)node;
                if (nestedClass.dict.ContainsKey("__type"))
                {
                    //is a special type, treat accordingly
                    switch (nestedClass["__type"].Value)
                    {
                    case "Date":
                        dataType = MoBackValueType.Date;
                        return(MoBackDate.DateFromMoBackJSON(node));

                    case "Pointer":
                        dataType = MoBackValueType.Pointer;
                        return(MoBackPointer.PointerFromMoBackJSON(node));

                    case "Relation":
                        dataType = MoBackValueType.Relation;
                        return(MoBackRelation.RelationFromMoBackJSON(node));

                    case "File":
                        dataType = MoBackValueType.File;
                        return(MoBackFile.FromJSON(node));

                    default:
                        //not familiar with this special type; fall back to parsing as regular object
                        if (MoBack.MoBackAppSettings.loggingLevel >= MoBack.MoBackAppSettings.LoggingLevel.WARNINGS)
                        {
                            Debug.LogWarning("Unrecognized MoBack reserved type '" + nestedClass["__type"] + "'.");
                        }
                        dataType = MoBackValueType.MoBackObject;
                        return(MoBackObject.FromJSON(nestedClass));
                    }
                }
                else
                {
                    dataType = MoBackValueType.MoBackObject;
                    return(MoBackObject.FromJSON(nestedClass));
                }
            }
            else if (node.valueType == typeof(SimpleJSONArray))
            {
                dataType = MoBackValueType.Array;
                return(MoBackArray.FromJSON((SimpleJSONArray)node));
            }

            throw new ArgumentException("JSON data type not supported in some manner.", "SimpleJsonNode node");
        }