1 package com.fasterxml.jackson.databind.misc; 2 3 import java.util.Arrays; 4 5 import com.fasterxml.jackson.databind.*; 6 import com.fasterxml.jackson.databind.util.JSONPObject; 7 import com.fasterxml.jackson.databind.util.JSONWrappedObject; 8 9 public class TestJSONP 10 extends BaseMapTest 11 { 12 static class Base { 13 public String a; 14 } 15 static class Impl extends Base { 16 public String b; 17 Impl(String a, String b)18 public Impl(String a, String b) { 19 this.a = a; 20 this.b = b; 21 } 22 } 23 24 private final ObjectMapper MAPPER = new ObjectMapper(); 25 testSimpleScalars()26 public void testSimpleScalars() throws Exception 27 { 28 assertEquals("callback(\"abc\")", 29 MAPPER.writeValueAsString(new JSONPObject("callback", "abc"))); 30 assertEquals("calc(123)", 31 MAPPER.writeValueAsString(new JSONPObject("calc", Integer.valueOf(123)))); 32 assertEquals("dummy(null)", 33 MAPPER.writeValueAsString(new JSONPObject("dummy", null))); 34 } 35 testSimpleBean()36 public void testSimpleBean() throws Exception 37 { 38 assertEquals("xxx({\"a\":\"123\",\"b\":\"456\"})", 39 MAPPER.writeValueAsString(new JSONPObject("xxx", 40 new Impl("123", "456")))); 41 } 42 43 /** 44 * Test to ensure that it is possible to force a static type for wrapped 45 * value. 46 */ testWithType()47 public void testWithType() throws Exception 48 { 49 Object ob = new Impl("abc", "def"); 50 JavaType type = MAPPER.constructType(Base.class); 51 assertEquals("do({\"a\":\"abc\"})", 52 MAPPER.writeValueAsString(new JSONPObject("do", ob, type))); 53 } 54 testGeneralWrapping()55 public void testGeneralWrapping() throws Exception 56 { 57 JSONWrappedObject input = new JSONWrappedObject("/*Foo*/", "\n// the end", 58 Arrays.asList()); 59 assertEquals("/*Foo*/[]\n// the end", MAPPER.writeValueAsString(input)); 60 } 61 } 62