/**
 *  Copyright 2005-2014 Red Hat, Inc.
 *
 *  Red Hat licenses this file to you under the Apache License, version
 *  2.0 (the "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 *  implied.  See the License for the specific language governing
 *  permissions and limitations under the License.
 */
package io.fabric8.insight.metrics.model;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;

public final class MetricsJSON {

    private static final SimpleDateFormat format;
    private static final ObjectMapper mapper;

    static {
        format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        mapper = new ObjectMapper();
        mapper.getDeserializationConfig().with(format);
        mapper.getSerializationConfig().with(format);
    }

    public static String toIso(Date d) {
        return format.format(d);
    }

    public static String toJson(Object o) {
        try {
            return mapper.writeValueAsString(o);
        } catch (Exception e) {
            throw new IllegalArgumentException("Could not serialize " + o, e);
        }
    }

    public static Map parseJson(String str) {
        try {
            return mapper.readValue(str, Map.class);
        } catch (Exception e) {
            throw new IllegalArgumentException("Could not deserialize " + str, e);
        }
    }

}

@@ -710,16 +710,45 @@
 Format;%0A
+import java.util.Collection;%0A
 import j
@@ -762,16 +762,16 @@
 l.Date;%0A
-
 import j
@@ -1050,9 +1050,9 @@
 .SSS
-Z
+X
 %22);%0A
@@ -1336,32 +1336,1204 @@
 %7B%0A        try %7B%0A
+            if (o instanceof Collection) %7B%0A                StringBuilder sb = new StringBuilder();%0A                sb.append(%22%5B%22);%0A                for (Object c : (Collection) o) %7B%0A                    if (sb.length() %3E 1) %7B%0A                        sb.append(%22,%22);%0A                    %7D%0A                    sb.append(toJson(c));%0A                %7D%0A                sb.append(%22%5D%22);%0A                return sb.toString();%0A            %7D else if (o instanceof Map) %7B%0A                StringBuilder sb = new StringBuilder();%0A                sb.append(%22%7B%22);%0A                for (Map.Entry%3CObject, Object%3E e : ((Map%3CObject, Object%3E) o).entrySet()) %7B%0A                    if (sb.length() %3E 1) %7B%0A                        sb.append(%22,%22);%0A                    %7D%0A                    sb.append(toJson(e.getKey().toString()));%0A                    sb.append(%22:%22);%0A                    sb.append(toJson(e.getValue()));%0A                %7D%0A                sb.append(%22%7D%22);%0A                return sb.toString();%0A            %7D else if (o == null) %7B%0A                return %22null%22;%0A            %7D else if (o instanceof Date) %7B%0A                return %22%5C%22%22 + toIso((Date) o) + %22%5C%22%22;%0A            %7D else %7B%0A    
             retu
@@ -2562,18 +2562,43 @@
 String(o
-);
+.toString());%0A            %7D
 %0A       

Fix timestamp formatting for insight metrics on elasticsearch