public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { Newtonsoft.Json.Linq.JObject item = Newtonsoft.Json.Linq.JObject.Load(reader); switch (item.Value <string>("type")) { case "roster": PlayerdataRoster _roster = new PlayerdataRoster(); serializer.Populate(item.CreateReader(), _roster); return(_roster); case "participant": PlayerdataParticipant @_participant = new PlayerdataParticipant(); serializer.Populate(item.CreateReader(), @_participant); return(@_participant); case "asset": PlayerdataAsset @_asset = new PlayerdataAsset(); serializer.Populate(item.CreateReader(), @_asset); return(@_asset); default: return(null); } }
/// <inheritdoc/> public override object?ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { Newtonsoft.Json.Linq.JObject jObject = serializer.Deserialize <Newtonsoft.Json.Linq.JObject>(reader); if (jObject == null) { return(null); } var discriminator = Newtonsoft.Json.Linq.Extensions.Value <string>(jObject.GetValue(this.discriminator, StringComparison.OrdinalIgnoreCase)); Type subtype = GetObjectSubtype(objectType, discriminator); var objectContract = serializer.ContractResolver.ResolveContract(subtype) as Newtonsoft.Json.Serialization.JsonObjectContract; if (objectContract == null || System.Linq.Enumerable.All(objectContract.Properties, p => p.PropertyName != this.discriminator)) { jObject.Remove(this.discriminator); } try { isReading = true; return(serializer.Deserialize(jObject.CreateReader(), subtype)); } finally { isReading = false; } }
/// <summary> /// Reads the JSON representation of the object. /// </summary> /// <param name="reader">The <see cref="Newtonsoft.Json.JsonReader"/> to read from.</param> /// <param name="objectType">Type of the object.</param> /// <param name="existingValue">The existing value of object being read.</param> /// <param name="serializer">The calling serializer.</param> /// <returns>The object value.</returns> public override object ReadJson(Newtonsoft.Json.JsonReader reader, Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { try { // Load JObject from stream Newtonsoft.Json.Linq.JObject jObject = Newtonsoft.Json.Linq.JObject.Load(reader); // Create target object based on JObject T target = Create(objectType, jObject); // Populate the object properties serializer.Populate(jObject.CreateReader(), target); return(target); } catch (Newtonsoft.Json.JsonSerializationException ex) { //TODO how to get this information up. throw ex; } catch (Exception ex) { //TODO how to get this information up. throw ex; } //Else ignore parameter return(null); }
static void Main(string[] args) { var path = Path.Combine(Directory.GetCurrentDirectory(), @"Report\DataModelSchema.json"); string builder = @""; try { using (StreamReader sr = new StreamReader(@"..\..\Report\DataModelSchema.txt")) { while (sr.Peek() >= 0) { builder += sr.ReadLine(); } } } catch (Exception e) { } string test = ""; foreach (char c in builder) { if (isWhatIWant(c)) { test += c; } } Newtonsoft.Json.Linq.JObject json = Newtonsoft.Json.Linq.JObject.Parse(test); JsonReader reader = json.CreateReader(); Console.WriteLine(); foreach (Newtonsoft.Json.Linq.JObject o in json["model"]["tables"]) { Console.WriteLine("Table Name: {0}", o["name"]); Console.WriteLine("Columns: "); foreach (Newtonsoft.Json.Linq.JObject y in o["columns"]) { Console.WriteLine(y["name"]); } Console.WriteLine(); } Console.ReadLine(); }