package com.topology.primitives.properties.converters.impl;

import com.topology.primitives.exception.properties.PropertyException;
import com.topology.primitives.properties.converters.PropertyConverter;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class MapConverter implements PropertyConverter<Map> {

  @Override
  public Map fromString(String input) throws PropertyException {
    if (input==null)
      throw new PropertyException("Input cannot be null");
    try {
      Map<String, String> map = new HashMap<String, String>();

      String[] nameValuePairs = input.trim().split("&");
      for (String nameValuePair : nameValuePairs) {
        String[] nameValue = nameValuePair.split("=");
        try {
          map.put(URLDecoder.decode(nameValue[0], "UTF-8"), nameValue.length > 1 ? URLDecoder.decode(
                  nameValue[1], "UTF-8") : "");
        } catch (UnsupportedEncodingException e) {
          throw new RuntimeException("This method requires UTF-8 encoding support", e);
        }
      }
      return map;
    } catch (NumberFormatException e) {
      throw new PropertyException("Input " + input + " is not a valid double string");
    }
  }

  @Override
  public String toString(Map input) throws PropertyException {
    if (input==null)
      throw new PropertyException("Input cannot be null");
    StringBuilder stringBuilder = new StringBuilder();
    Map<String, String> map = input;
    for (String key : map.keySet()) {
      if (stringBuilder.length() > 0) {
        stringBuilder.append("&");
      }
      String value = map.get(key);
      try {
        stringBuilder.append((key != null ? URLEncoder.encode(key, "UTF-8") : ""));
        stringBuilder.append("=");
        stringBuilder.append(value != null ? URLEncoder.encode(value, "UTF-8") : "");
      } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("This method requires UTF-8 encoding support", e);
      }
    }

    return stringBuilder.toString();
  }
}

@@ -197,16 +197,90 @@
 verter;%0A
+import org.codehaus.jackson.map.ObjectMapper;%0A%0Aimport java.io.IOException;
 %0Aimport 
@@ -650,184 +650,57 @@
     
-try %7B%0A      Map%3CString, String%3E
+ObjectMapper
  map
+per
  = new 
-HashMap%3CString, String%3E();%0A%0A      String%5B%5D nameValuePairs = input.trim().split(%22&%22);%0A      for (String nameValuePair : nameValuePairs)
+ObjectMapper();%0A    try
  %7B%0A 
@@ -708,229 +708,55 @@
     
- 
- String%5B%5D nameValue = nameValuePair.split(%22=%22);%0A        try %7B%0A          map.put(URLDecoder.decode(nameValue%5B0%5D, %22UTF-8%22), nameValue.length %3E 1 ? URLDecoder.decode(%0A                  nameValue%5B1%5D, %22UTF-8%22) : %22%22
+return mapper.readValue(input, Map.class
 );%0A    
- 
-   
 %7D ca
@@ -760,35 +760,18 @@
  catch (
-UnsupportedEncoding
+IO
 Exceptio
@@ -773,28 +773,24 @@
 eption e) %7B%0A
-    
       throw 
@@ -785,39 +785,40 @@
       throw new 
-Runtime
+Property
 Exception(%22This 
@@ -816,641 +816,302 @@
 on(%22
-This method requires UTF-8 encoding support%22, e);%0A        %7D%0A      %7D%0A      return map;%0A    %7D catch (NumberFormatException e) %7B%0A      throw new PropertyException(%22Input %22 + input + %22 is not a valid double string%22);%0A    %7D%0A  %7D%0A%0A  @Override%0A  public String toString(Map input) throws PropertyException %7B%0A    if (input==null)%0A      throw new PropertyException(%22Input cannot be null%22);%0A    StringBuilder stringBuilder = new StringBuilder();%0A    Map%3CString, String%3E map = input;%0A    for (String key : map.keySet()) %7B%0A      if (stringBuilder.length() %3E 0) %7B%0A        stringBuilder.append(%22&%22);%0A      %7D%0A      String value = map.get(key
+Error in converting string value to map property : %22 + e.getMessage());%0A    %7D%0A  %7D%0A%0A  @Override%0A  public String toString(Map input) throws PropertyException %7B%0A    if (input==null)%0A      throw new PropertyException(%22Input cannot be null%22);%0A    ObjectMapper objectMapper = new ObjectMapper(
 );%0A    
-  
 try 
@@ -1122,209 +1122,55 @@
     
-  
-stringBuilder.append((key != null ? URLEncoder.encode(key, %22UTF-8%22) : %22%22));%0A        stringBuilder.append(%22=%22);%0A        stringBuilder.append(value != null ? URLEncoder.encode(value, %22UTF-8%22) : %22%22
+return objectMapper.writeValueAsString(input
 );%0A
-  
     
@@ -1178,35 +1178,18 @@
  catch (
-UnsupportedEncoding
+IO
 Exceptio
@@ -1197,26 +1197,24 @@
  e) %7B%0A      
-  
 throw new Ru
@@ -1215,15 +1215,16 @@
 new 
-Runtime
+Property
 Exce
@@ -1234,109 +1234,83 @@
 on(%22
-This method requires UTF-8 encoding support%22, e);%0A      %7D%0A    %7D%0A%0A    return stringBuilder.toString();
+Error in converting map property to JSON string: %22 + e.getMessage());%0A    %7D
 %0A  %7D

Update MapConverter to use JSON serialization and deserialization