package com.alibaba.json.bvt.issue_2400;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import junit.framework.TestCase;

import java.util.LinkedHashMap;
import java.util.Map;

public class Issue2447 extends TestCase {

    public void test_for_issue() {
        VO vo = new VO();
        vo.id = 123;
        vo.location = new Location(127, 37);

        Object obj = JSON.toJSON(vo);
        String json = JSON.toJSONString(vo);
        assertEquals("{\"latitude\":37,\"id\":123,\"longitude\":127}", obj.toString());
    }

    public void test_for_issue2() {
        VO2 vo = new VO2();
        vo.id = 123;
        vo.properties.put("latitude", 37);
        vo.properties.put("longitude", 127);

        Object obj = JSON.toJSON(vo);
        assertEquals("{\"latitude\":37,\"id\":123,\"longitude\":127}", obj.toString());
    }

    public static class VO {

        public int id;

        @JSONField(unwrapped = true)
        public Location location;
    }

    public static class VO2 {
        public int id;

        @JSONField(unwrapped = true)
        public Map<String, Object> properties = new LinkedHashMap<String, Object>();
    }


    public static class Location {
        public int longitude;
        public int latitude;

        public Location() {}

        public Location(int longitude, int latitude) {
            this.longitude = longitude;
            this.latitude = latitude;
        }
    }
}

@@ -119,16 +119,74 @@
 NField;%0A
+import com.alibaba.fastjson.serializer.SerializerFeature;%0A
 import j
@@ -210,16 +210,16 @@
 stCase;%0A
-
 %0Aimport 
@@ -490,20 +490,20 @@
  String 
-json
+text
  = JSON.
@@ -515,18 +515,48 @@
 NString(
-v
 o
+bj, SerializerFeature.SortField
 );%0A     
@@ -567,32 +567,43 @@
 ssertEquals(%22%7B%5C%22
+id%5C%22:123,%5C%22
 latitude%5C%22:37,%5C%22
@@ -595,35 +595,24 @@
 atitude%5C%22:37
-,%5C%22id%5C%22:123
 ,%5C%22longitude
@@ -617,38 +617,28 @@
 de%5C%22:127%7D%22, 
-obj.toString()
+text
 );%0A    %7D%0A%0A  
@@ -839,32 +839,107 @@
 SON.toJSON(vo);%0A
+        String text = JSON.toJSONString(obj, SerializerFeature.SortField);%0A
         assertEq
@@ -947,16 +947,27 @@
 als(%22%7B%5C%22
+id%5C%22:123,%5C%22
 latitude
@@ -975,19 +975,8 @@
 %22:37
-,%5C%22id%5C%22:123
 ,%5C%22l
@@ -997,22 +997,12 @@
 %7D%22, 
-obj.toString()
+text
 );%0A 

Fix flaky testcases for issue #2447