コード例 #1
0
 public FirestormDocumentForCommit(string path, FirestormDocument fd)
 {
     this.name   = path;
     this.fields = fd.fields;
 }
コード例 #2
0
        public FirestormDocumentSnapshot(string jsonString)
        {
            Debug.Log($"Snapshot from {jsonString}");
            //File.WriteAllText(Application.dataPath + $"/{UnityEngine.Random.Range(0, 100)}.txt", jsonString);
            IsEmpty = false;

            JsonReader specialReader = new JsonReader(jsonString);

            specialReader.ObjectAsDictString = true;
            this.document = JsonMapper.ToObject <FirestormDocument>(specialReader);

            //Write in a format that can be map to any object by (a modified) LitJSON
            var writer = new JsonWriter();

            writer.PrettyPrint = true;
            writer.WriteObjectStart();
            foreach (var field in document.fields.Keys)
            {
                var insideValueText = document.fields[field].First().Key;
                var insideValue     = document.fields[field].First().Value;
                writer.WritePropertyName(field);
                ValueTextToWrite(insideValueText, insideValue);
            }

            writer.WriteObjectEnd();

            formattedDataJson = writer.ToString();
            Debug.Log($"Formatted {formattedDataJson}");

            void ValueTextToWrite(string valueText, object value)
            {
                if (value is JsonData jd)
                {
                    value = (JsonData)jd;
                }

                switch (valueText)
                {
                case "integerValue":
                    //Integer is dangerous because it came as string of number
                    writer.Write(int.Parse(Convert.ToString(value)));
                    break;

                case "doubleValue":
                    //Debug.Log($"Hey! Casting {value.GetType().Name} !");
                    writer.Write(Convert.ToDouble(value));
                    break;

                case "booleanValue":
                    writer.Write(Convert.ToBoolean(value));
                    break;

                case "bytesValue":
                    writer.Write(Convert.ToString(value));
                    break;

                case "arrayValue":
                    writer.WriteArrayStart();
                    JsonData al = (JsonData)((ValueObject)value)["values"];
                    foreach (JsonData a in al)
                    {
                        //If you put array in array it may explode here
                        ValueTextToWrite(a.Keys.First(), a[a.Keys.First()].UnderlyingPrimitive());
                    }

                    writer.WriteArrayEnd();
                    break;

                case "mapValue":
                    writer.WriteObjectStart();
                    JsonData alObj = (JsonData)((ValueObject)value)["fields"];
                    //Debug.Log($"{alObj} {alObj.Count} {alObj.IsArray} {alObj.IsObject}");
                    foreach (KeyValuePair <string, JsonData> a in alObj)
                    {
                        writer.WritePropertyName(a.Key);
                        JsonData mapFieldData = a.Value;
                        var      firstKey     = mapFieldData.Keys.First();
                        //Debug.Log($"Working on {a.Key} {firstKey} {mapFieldData[firstKey].UnderlyingPrimitive()} object map");
                        ValueTextToWrite(firstKey, mapFieldData[firstKey].UnderlyingPrimitive());
                    }

                    writer.WriteObjectEnd();
                    break;

                default:
                    //Debug.Log($"AHA {valueText} {value} {value?.GetType().Name}");
                    string casted = (string)value;
                    writer.Write(casted);
                    //Debug.Log($"AHAhh {valueText} {value}");
                    break;
                }
            }
        }