/// <summary> /// deserialize a json string to a smartObject instance /// </summary> /// <param name="obj">json string</param> /// <returns>Smart Object instance</returns> public static SmartObject DeserializeObject(string obj) { SmartObject.Builder builder = new SmartObject.Builder(); Dictionary <string, object> attributes = new Dictionary <string, object>(); var rawObject = JsonConvert.DeserializeObject <Dictionary <string, object> >(obj, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, DateFormatString = EventSerializer.DatetimeFormat, DateTimeZoneHandling = DateTimeZoneHandling.Utc }); foreach (KeyValuePair <string, object> token in rawObject) { switch (token.Key) { case DeviceIdProperty: { if (!(token.Value is string)) { throw new InvalidOperationException("Field 'x_device_id' does not match TYPE 'TEXT'"); } builder.DeviceId = token.Value as string; break; } case RegistrationDateProperty: { if (token.Value == null || !(token.Value is DateTime)) { throw new InvalidOperationException("Field 'x_registration_date' does not match TYPE 'DATETIME'"); } builder.RegistrationDateTime = new DateTimeOffset((DateTime)token.Value); break; } case ObjectTypeProperty: { if (!(token.Value is string)) { throw new InvalidOperationException("Field 'x_object_type' does not match TYPE 'TEXT'"); } builder.ObjectType = token.Value as string; break; } case OwnerProperty: { if (token.Value == null || !(token.Value is JObject)) { throw new InvalidOperationException("Field 'x_owner' does not match TYPE 'OWNER'"); } var username = (token.Value as JObject)[OwnerSerializer.UsernameProperty]; if (username != null) { if (username.Type != JTokenType.String) { throw new InvalidOperationException("Field 'x_owner' does not match TYPE 'OWNER'"); } builder.Username = username.Value <string>(); } break; } default: { if (token.Value is JArray) { attributes.Add(token.Key, (token.Value as JArray).ToObject <string[]>()); } else { attributes.Add(token.Key, token.Value); } break; } } } builder.Attributes = attributes; return(builder.Build()); }
/// <summary> /// deserialize a json string to a smartObject instance /// </summary> /// <param name="obj">json string</param> /// <returns>Smart Object instance</returns> public static SmartObject DeserializeSmartObject(string obj) { SmartObject.Builder builder = new SmartObject.Builder(); Dictionary <string, object> attributes = new Dictionary <string, object>(); foreach (KeyValuePair <string, object> token in JsonConvert.DeserializeObject <Dictionary <string, object> >(obj)) { switch (token.Key) { case DeviceIdProperty: { if (!(token.Value is string)) { throw new InvalidOperationException("Field 'x_device_id' does not match TYPE 'TEXT'"); } builder.DeviceId = token.Value as string; break; } case RegistrationDateProperty: { if (token.Value == null || !(token.Value is DateTime)) { throw new InvalidOperationException("Field 'x_registration_date' does not match TYPE 'DATETIME'"); } builder.RegistrationDate = ((DateTime)token.Value).ToUniversalTime(); break; } case ObjectTypeProperty: { if (!(token.Value is string)) { throw new InvalidOperationException("Field 'x_object_type' does not match TYPE 'TEXT'"); } builder.ObjectType = token.Value as string; break; } case EventIdProperty: { Guid guid; if (!(token.Value is string) || string.IsNullOrEmpty(token.Value as string) || !(Guid.TryParse(token.Value as string, out guid))) { throw new InvalidOperationException("Field 'x_event_id' does not match TYPE 'GUID'"); } builder.EventId = guid; break; } case OwnerProperty: { if (token.Value == null || !(token.Value is JObject)) { throw new InvalidOperationException("Field 'x_owner' does not match TYPE 'OWNER'"); } var username = (token.Value as JObject)[OwnerSerializer.UsernameProperty]; if (username != null) { if (username.Type != JTokenType.String) { throw new InvalidOperationException("Field 'x_owner' does not match TYPE 'OWNER'"); } builder.Username = username.Value <string>(); } break; } default: { if (token.Value is JArray) { attributes.Add(token.Key, (token.Value as JArray).ToObject <string[]>()); } else { attributes.Add(token.Key, token.Value); } break; } } } builder.Attributes = attributes; return(builder.Build()); }