private void SerializeDictionaryGeneric <TValue>(ISerializerNode node, Writer writer, DictionaryTypeSpec dictType) { var jsonWriter = writer.JsonWriter; var dict = (IDictionary <string, TValue>)node.Value; var expectedValueType = dictType.ValueType; jsonWriter.WriteStartObject(); var dictDelta = dict as IDictionaryDelta <string, TValue>; var serializedKeyValuePairs = dictDelta != null ? dictDelta.ModifiedItems : dict; if (dictDelta != null && dictDelta.RemovedKeys.Any()) { foreach (var removed in dictDelta.RemovedKeys) { jsonWriter.WritePropertyName("-" + EscapePropertyName(removed)); jsonWriter.WriteStartObject(); jsonWriter.WriteEndObject(); } } foreach (var kvp in serializedKeyValuePairs) { // TODO: Support other key types than string jsonWriter.WritePropertyName(EscapePropertyName(kvp.Key)); var itemNode = new ItemValueSerializerNode(kvp.Value, expectedValueType, node.ExpandPath, node.Context, node); SerializeThroughContext(itemNode, writer); } jsonWriter.WriteEndObject(); }
private void DeserializeDictionaryGeneric <TValue>(IDeserializerNode node, Reader reader, DictionaryTypeSpec dictType) { IDictionary <string, TValue> dict; if (node.Value != null) { dict = (IDictionary <string, TValue>)node.Value; } else { dict = new Dictionary <string, TValue>(); } var jobj = reader.Token as JObject; var valueType = dictType.ValueType; if (jobj == null) { throw new PomonaSerializationException( "Expected dictionary property to have a JSON object value, but was " + reader.Token.Type); } foreach (var jprop in jobj.Properties()) { var jpropName = jprop.Name; if (jpropName.Length > 0 && reservedFirstCharacters.Contains(jpropName[0])) { if (jpropName[0] == '-') { // Removal operation var unescapedPropertyName = UnescapePropertyName(jpropName.Substring(1)); dict.Remove(unescapedPropertyName); } else { throw new PomonaSerializationException( "Unexpected character in json property name. Have propertie names been correctly escaped?"); } } else { var unescapedPropertyName = UnescapePropertyName(jpropName); var itemNode = new ItemValueDeserializerNode(valueType, node.Context, node.ExpandPath + "." + unescapedPropertyName); DeserializeThroughContext(itemNode, new Reader(jprop.Value)); dict[unescapedPropertyName] = (TValue)itemNode.Value; } } if (node.Value == null) { node.Value = dict; } }