Exemplo n.º 1
0
        public override void WriteJson(JsonWriter writer, PluginData value, Newtonsoft.Json.JsonSerializer serializer)
        {
            writer.WriteStartObject();

            if (value.Data == null)
            {
                writer.WritePropertyName("Data");
                writer.WriteNull();
            }

            else
            {
                writer.WritePropertyName("Data");
                writer.WriteStartObject();

                foreach (KeyValuePair <string, Tuple <Type, object> > keyValuePair in value.Data)
                {
                    writer.WritePropertyName(keyValuePair.Key);
                    writer.WriteStartObject();

                    writer.WritePropertyName("Item1");
                    writer.WriteValue(keyValuePair.Value.Item1.FullName);

                    writer.WritePropertyName("Item2");
                    serializer.Serialize(writer, keyValuePair.Value.Item2);

                    writer.WriteEndObject();
                }

                writer.WriteEndObject();
            }

            writer.WriteEndObject();
        }
 public void Setup()
 {
     _instance                   = new T();
     _newtonSoftSerializer       = new Newtonsoft.Json.JsonSerializer();
     _dataContractJsonSerializer = new DataContractJsonSerializer(typeof(T));
     _memoryStream               = new MemoryStream(capacity: short.MaxValue);
     _streamWriter               = new StreamWriter(_memoryStream, Encoding.UTF8);
     _utf8JsonWriter             = new Utf8JsonWriter(_memoryStream);
 }
Exemplo n.º 3
0
        public override PluginData ReadJson(JsonReader reader, Type objectType, PluginData existingValue, bool hasExistingValue,
                                            Newtonsoft.Json.JsonSerializer serializer)
        {
            PluginData pluginData = new PluginData();

            reader.Read();
            reader.Read();

            pluginData.Data = reader.TokenType == JsonToken.Null ? null : new Dictionary <string, Tuple <Type, object> >();

            reader.Read();

            while (reader.TokenType != JsonToken.EndObject)
            {
                string key = reader.Value as string;

                reader.Read();
                reader.Read();
                reader.Read();

                string ty   = reader.Value as string;
                Type   type = Type.GetType(ty);

                if (type == null)
                {
                    type = AppDomain.CurrentDomain.GetAssemblies()
                           .FirstOrDefault(a => !a.IsDynamic && a.GetType(ty) != null)
                           ?.GetType(ty);

                    if (type == null)
                    {
                        throw new Exception("Unable to find type while trying to convert JSON to PluginData object: " + ty);
                    }
                }

                reader.Read();
                reader.Read();

                pluginData.Data?.Add(key, new Tuple <Type, object>(type, serializer.Deserialize(reader, type)));

                reader.Read();
                reader.Read();
            }

            reader.Read();
            reader.Read();

            return(pluginData);
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            while (true)
            {
                string input = Console.ReadLine();

                Console.WriteLine("");

                switch (input)
                {
                case "wc":
                {
                    model = new GameModel();

                    GameObject o = new Meteor();
                    model.Add(o);

                    (o.X, o.Y) = (100, 200);

                    string json = JsonConvert.SerializeObject(model, Formatting.Indented);

                    StreamWriter file = new StreamWriter("JSON.json", false);

                    Console.WriteLine(json);
                    file.WriteLine(json);

                    file.Close();
                }

                break;

                case "ws":
                {
                    model = new GameModel();

                    GameObject o = new Meteor();
                    model.Add(o);

                    (o.X, o.Y) = (100, 200);

                    string json = JsonSerializer.Serialize(model, new JsonSerializerOptions {
                            WriteIndented = true,
                        });

                    StreamWriter file = new StreamWriter("JSON.json", false);

                    Console.WriteLine(json);
                    file.WriteLine(json);

                    file.Close();
                }

                break;


                case "wsj":
                {
                    model = new GameModel();

                    GameObject o = new Meteor();
                    model.Add(o);

                    (o.X, o.Y) = (100, 200);

                    var jsonSerializer = new Newtonsoft.Json.JsonSerializer();
                    jsonSerializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                    jsonSerializer.TypeNameHandling      = TypeNameHandling.Auto;
                    jsonSerializer.Formatting            = Formatting.Indented;

                    StreamWriter wfile      = new StreamWriter("JSON.json", false);
                    JsonWriter   jsonWriter = new JsonTextWriter(wfile);

                    jsonSerializer.Serialize(jsonWriter, model);

                    jsonWriter.Close();
                    wfile.Close();



                    StreamReader rfile = new StreamReader("JSON.json");
                    string       json  = rfile.ReadToEnd();
                    rfile.Close();

                    Console.WriteLine(json);
                }

                break;

                case "r":

                {
                    StreamReader file = new StreamReader("JSON.json");

                    string json = file.ReadToEnd();

                    GameModel model = JsonSerializer.Deserialize <GameModel>(json);

                    GameObject o = model.gObjects[0];

                    json = JsonSerializer.Serialize(model, new JsonSerializerOptions {
                            WriteIndented = true,
                        });

                    Console.WriteLine(json);
                }

                break;


                case "rsj":
                {
                    var jsonSerializer = new Newtonsoft.Json.JsonSerializer();
                    jsonSerializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                    jsonSerializer.TypeNameHandling      = TypeNameHandling.Auto;
                    jsonSerializer.Formatting            = Formatting.Indented;

                    StreamReader file       = new StreamReader("JSON.json");
                    JsonReader   jsonReader = new JsonTextReader(file);

                    model = jsonSerializer.Deserialize <GameModel>(jsonReader);

                    jsonReader.Close();
                    file.Close();



                    GameObject o = model.gObjects[0];

                    string json = JsonSerializer.Serialize(model, new JsonSerializerOptions {
                            WriteIndented = true,
                        });

                    Console.WriteLine(json);
                }

                break;
                }

                Console.Write("\n\n");
            }
        }