Search in sources :

Example 1 with JsonFormat

use of org.nutz.json.JsonFormat in project nutzboot by nutzam.

the class FeignStarter method getEncoder.

protected Encoder getEncoder(FeignInject fc, Field field) {
    String encoderStr = Strings.sBlank(fc.encoder(), conf.get(PROP_ENCODER, ""));
    switch(encoderStr) {
        case "":
        case "raw":
            break;
        case "nutzjson":
            JsonFormat jf = JsonFormat.full();
            String jfStr = Strings.sBlank(fc.jsonFormat(), conf.get(PROP_JSON_FORMAT, ""));
            if (!Strings.isBlank(jfStr)) {
                if (jfStr.startsWith("{")) {
                    jf = Json.fromJson(JsonFormat.class, jfStr);
                } else if (jfStr.startsWith("ioc:")) {
                    jf = ioc.get(JsonFormat.class, jfStr.substring(4));
                } else {
                    try {
                        jf = (JsonFormat) JsonFormat.class.getMethod(jfStr).invoke(null);
                    } catch (Exception e) {
                        log.infof("invaild JsonFormat=[%s] at %s", jfStr, field);
                    }
                }
            }
            return new NutzJsonEncoder(jf);
        case "jackson":
            return new JacksonEncoder();
        case "gson":
            return new GsonEncoder();
        case "jaxb":
            JAXBContextFactory jaxbFactory = new JAXBContextFactory.Builder().withMarshallerJAXBEncoding("UTF-8").withMarshallerSchemaLocation(getSchemaString(fc.schema())).build();
            return new JAXBEncoder(jaxbFactory);
        default:
            if (encoderStr.startsWith("ioc"))
                return ioc.get(Encoder.class, encoderStr.substring(4));
            break;
    }
    return null;
}
Also used : JAXBContextFactory(feign.jaxb.JAXBContextFactory) JsonFormat(org.nutz.json.JsonFormat) GsonEncoder(feign.gson.GsonEncoder) JAXBEncoder(feign.jaxb.JAXBEncoder) Encoder(feign.codec.Encoder) JacksonEncoder(feign.jackson.JacksonEncoder) GsonEncoder(feign.gson.GsonEncoder) JAXBEncoder(feign.jaxb.JAXBEncoder) JacksonEncoder(feign.jackson.JacksonEncoder)

Example 2 with JsonFormat

use of org.nutz.json.JsonFormat in project nutz by nutzam.

the class MaplTest method testIssue322.

@Test
public void testIssue322() {
    String json = "{name:'nutz', age:12, address:[{area:1,name:'abc'},{area:2,name:'123'}]}";
    Object obj = Json.fromJson(json);
    Object newobj = Mapl.excludeFilter(obj, Lang.list("age", "address[].area"));
    JsonFormat jf = new JsonFormat(true);
    assertEquals("{\"name\":\"nutz\",\"address\":[{\"name\":\"abc\"}, {\"name\":\"123\"}]}", Json.toJson(newobj, jf));
}
Also used : JsonFormat(org.nutz.json.JsonFormat) Test(org.junit.Test)

Aggregations

JsonFormat (org.nutz.json.JsonFormat)2 Encoder (feign.codec.Encoder)1 GsonEncoder (feign.gson.GsonEncoder)1 JacksonEncoder (feign.jackson.JacksonEncoder)1 JAXBContextFactory (feign.jaxb.JAXBContextFactory)1 JAXBEncoder (feign.jaxb.JAXBEncoder)1 Test (org.junit.Test)1