static void Main(string[] args) { //var json = new JsonArray( // new JsonObject( // new JsonMember("A", new JsonNumber(1)), // new JsonMember("B", new JsonNumber(2)), // new JsonMember("C", new JsonArray(new JsonNumber(1), new JsonNumber(2)))), // new JsonObject( // new JsonMember("D", new JsonNumber(3)), // new JsonMember("E", new JsonNumber(4)))); var nested = new JsonObject( new JsonMember("A", new JsonNumber(3)), new JsonMember("A", new JsonNumber(3.01m)), new JsonMember("A", new JsonNumber(3))); var array = new JsonArray(nested, nested, nested); var array2 = new JsonArray(new JsonNumber(3), new JsonNumber(2), new JsonNumber(1)); JsonValue json = new JsonObject( new JsonMember("A", new JsonString("\u0460\u849c\u8089")), new JsonMember("B", new JsonNumber(2)), new JsonMember("C", new JsonObject( new JsonMember("A", new JsonNumber(3)), new JsonMember("A", new JsonNumber(3)), new JsonMember("A", new JsonNumber(3)), new JsonMember("ComplexArray", array), new JsonMember("SimpleArray", array2)))); Console.WriteLine(json.Stringify(true)); json = Json.CreateAst(json.Stringify(true)); Console.WriteLine(json.Stringify(true)); }
/// <summary> /// Deserialize a JSON object. /// </summary> /// <param name="serializer">The serializer to utilize when deserializing nested objects.</param> /// <param name="type">The type of the object to deserialize to.</param> /// <param name="jsonObject">The JSON object to deserialize from.</param> /// <returns>The CLR object that represents the JSON object.</returns> static object DeserializeObject(IJsonSerializer serializer, Type type, JsonObject jsonObject) { var entity = Activator.CreateInstance(type); foreach (var member in jsonObject.Members) { var property = type.GetRuntimeProperty(serializer.FieldNamingStrategy.ResolveName(member.Name)); property?.SetValue(entity, serializer.DeserializeValue(property.PropertyType, member.Value)); } return entity; }
/// <summary> /// Attempts to adds an entity to the <see cref="T:Hypermedia.JsonApi.IJsonApiEntityCache" />. /// </summary> /// <param name="cache">The cache to add the entity to.</param> /// <param name="key">The item that defines the key for the entity to add.</param> /// <param name="value">The object to use as the value of the element to add.</param> /// <returns>true if the entity could be added, false if it could not be added.</returns> public static bool TryAdd(this IJsonApiEntityCache cache, JsonObject key, object value) { if (cache == null) { throw new ArgumentNullException(nameof(cache)); } string type; string id; if (TryCreateKey(key, out type, out id) == false) { return false; } return cache.TryAdd(type, id, value); }
/// <summary> /// Visit the JSON object. /// </summary> /// <param name="jsonObject">The JSON object to visit.</param> /// <returns>The type that was visited.</returns> protected override string Visit(JsonObject jsonObject) { _depth += 2; var builder = new StringBuilder().Append("{").NewLine().Indent(_depth); for (var i = 0; i < jsonObject.Members.Count; i++) { builder.Append(VisitMember(jsonObject.Members[i])); if (i < jsonObject.Members.Count - 1) { builder.Append(", ").NewLine().Indent(_depth); } } _depth -= 2; builder.NewLine().Indent(_depth).Append("}"); return builder.ToString(); }
/// <summary> /// Deserialize into the given instance. /// </summary> /// <param name="serializer">The serializer to utilize when deserializing nested objects.</param> /// <param name="jsonObject">The JSON object to deserialize from.</param> /// <param name="contract">The contract for the type that is being deserialized.</param> /// <param name="instance">The instance to deserialize into.</param> internal void DeserializeObject(IJsonSerializer serializer, JsonObject jsonObject, IContract contract, object instance) { DeserializeFields(serializer, jsonObject, contract.Fields.Where(ShouldDeserializeField).ToList(), instance); }
/// <summary> /// Deserialize the fields for the given instance. /// </summary> /// <param name="serializer">The serializer to utilize when deserializing nested objects.</param> /// <param name="jsonObject">The JSON object to deserialize from.</param> /// <param name="fields">The list of fields to deserialize.</param> /// <param name="instance">The instance to deserialize into.</param> void DeserializeFields(IJsonSerializer serializer, JsonObject jsonObject, IReadOnlyList<IField> fields, object instance) { foreach (var member in jsonObject.Members) { var field = fields.SingleOrDefault(f => String.Equals(f.Name, member.Name, StringComparison.OrdinalIgnoreCase)); field?.SetValue(instance, serializer.DeserializeValue(field.Accessor.ValueType, member.Value)); } }
/// <summary> /// Attempts to create the key components of an entity. /// </summary> /// <param name="jsonObject">The JSON object to create the key for.</param> /// <param name="type">The type of the entity.</param> /// <param name="id">The ID of the entity.</param> /// <returns>true if the required components could be created, false if not.</returns> static bool TryCreateKey(JsonObject jsonObject, out string type, out string id) { type = null; id = null; if (jsonObject["id"] == null) { return false; } type = jsonObject["type"].Stringify().ToLower(); id = jsonObject["id"].Stringify().ToLower(); return true; }