package com.ctrip.ops.sysdev.decoder;

import java.util.HashMap;
import java.util.Map;

import org.joda.time.DateTime;
import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;

import org.apache.log4j.Logger;

public class JsonDecoder implements IDecode {
    private static final Logger logger = Logger
            .getLogger(JsonDecoder.class.getName());

    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> decode(final String message) {
        Map<String, Object> event = null;
        try {
            event = (HashMap<String, Object>) JSONValue
                    .parseWithException(message);
        } catch (Exception e) {
            logger.warn("failed to json parse message to a Map");
            event = new HashMap<String, Object>() {
                {
                    put("message", message);
                    put("@timestamp", DateTime.now());
                }
            };
        }

        if (event == null) {
            event = new HashMap<String, Object>() {
                {
                    put("message", message);
                    put("@timestamp", DateTime.now());
                }
            };
        } else {
            if (!event.containsKey("@timestamp")) {
                event.put("@timestamp", DateTime.now());
            }
        }
        return event;
    }
}

@@ -81,16 +81,47 @@
 l.Map;%0A%0A
+import org.jcodings.util.Hash;%0A
 import o
@@ -570,59 +570,54 @@
     
-try %7B%0A            event = (HashMap%3CString, Object%3E)
+Object e = null;%0A        try %7B%0A            e =
  JSO
@@ -690,16 +690,21 @@
  catch (
+Parse
 Exceptio
@@ -706,16 +706,24 @@
 eption e
+xception
 ) %7B%0A    
@@ -775,18 +775,67 @@
 sage
- to a Map%22
+%22);%0A            logger.warn(exception.getLocalizedMessage()
 );%0A 
@@ -1292,24 +1292,99 @@
    %7D else %7B%0A
+            try %7B%0A                event = (HashMap%3CString, Object%3E) e;%0A    
             
@@ -1419,24 +1419,28 @@
 estamp%22)) %7B%0A
+    
             
@@ -1476,32 +1476,453 @@
 ateTime.now());%0A
+                %7D%0A            %7D catch (Exception exception) %7B%0A                logger.warn(%22failed to convert event to Map object%22);%0A                logger.warn(exception.getLocalizedMessage());%0A                event = new HashMap%3CString, Object%3E() %7B%0A                    %7B%0A                        put(%22message%22, message);%0A                        put(%22@timestamp%22, DateTime.now());%0A                    %7D%0A                %7D;%0A
             %7D%0A  
@@ -1921,32 +1921,33 @@
     %7D%0A        %7D%0A
+%0A
         return e

split json.parse and convert to map