Example usage for com.fasterxml.jackson.dataformat.xml XmlMapper setSerializerProvider

List of usage examples for com.fasterxml.jackson.dataformat.xml XmlMapper setSerializerProvider

Introduction

In this page you can find the example usage for com.fasterxml.jackson.dataformat.xml XmlMapper setSerializerProvider.

Prototype

public ObjectMapper setSerializerProvider(DefaultSerializerProvider p) 

Source Link

Document

Method for setting specific SerializerProvider to use for handling caching of JsonSerializer instances.

Usage

From source file:com.ning.billing.recurly.model.RecurlyObject.java

public static XmlMapper newXmlMapper() {
    final XmlMapper xmlMapper = new XmlMapper();
    xmlMapper.setSerializerProvider(new RecurlyXmlSerializerProvider());
    final AnnotationIntrospector primary = new JacksonAnnotationIntrospector();
    final AnnotationIntrospector secondary = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
    final AnnotationIntrospector pair = new AnnotationIntrospectorPair(primary, secondary);
    xmlMapper.setAnnotationIntrospector(pair);
    xmlMapper.registerModule(new JodaModule());
    xmlMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

    final SimpleModule m = new SimpleModule("module", new Version(1, 0, 0, null, null, null));
    m.addSerializer(Accounts.class, new RecurlyObjectsSerializer<Accounts, Account>(Accounts.class, "account"));
    m.addSerializer(AddOns.class, new RecurlyObjectsSerializer<AddOns, AddOn>(AddOns.class, "add_on"));
    m.addSerializer(Adjustments.class,
            new RecurlyObjectsSerializer<Adjustments, Adjustment>(Adjustments.class, "adjustment"));
    m.addSerializer(Coupons.class, new RecurlyObjectsSerializer<Coupons, Coupon>(Coupons.class, "coupon"));
    m.addSerializer(Invoices.class, new RecurlyObjectsSerializer<Invoices, Invoice>(Invoices.class, "invoice"));
    m.addSerializer(Plans.class, new RecurlyObjectsSerializer<Plans, Plan>(Plans.class, "plan"));
    m.addSerializer(RecurlyErrors.class,
            new RecurlyObjectsSerializer<RecurlyErrors, RecurlyError>(RecurlyErrors.class, "error"));
    m.addSerializer(SubscriptionAddOns.class,
            new RecurlyObjectsSerializer<SubscriptionAddOns, SubscriptionAddOn>(SubscriptionAddOns.class,
                    "subscription_add_on"));
    m.addSerializer(Subscriptions.class,
            new RecurlyObjectsSerializer<Subscriptions, Subscription>(Subscriptions.class, "subscription"));
    m.addSerializer(Transactions.class,
            new RecurlyObjectsSerializer<Transactions, Transaction>(Transactions.class, "transaction"));
    xmlMapper.registerModule(m);// w w  w. ja v  a 2 s  .  c om

    return xmlMapper;
}

From source file:com.ning.billing.recurly.model.TestXmlMapper.java

@Test(groups = "fast", description = "See https://github.com/FasterXML/jackson-dataformat-xml/issues/76")
public void testCollection() throws Exception {
    final XmlMapper xmlMapper = new XmlMapper();
    xmlMapper.setSerializerProvider(new RecurlyXmlSerializerProvider());
    final SimpleModule m = new SimpleModule("module", new Version(1, 0, 0, null, null, null));
    m.addSerializer(Values.class, new ValuesSerializer());
    xmlMapper.registerModule(m);/* w w w.ja  v  a2 s  .c om*/

    final Values values = xmlMapper
            .readValue(
                    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "  <values type=\"array\">\n"
                            + "    <value>Hi!</value>" + "    <value>Salut!</value>" + "  </values>",
                    Values.class);
    Assert.assertEquals(values.size(), 2, values.toString());
    Assert.assertEquals(values.get(0), "Hi!");
    Assert.assertEquals(values.get(1), "Salut!");

    // Test we can re-serialize
    final String valueAsString = xmlMapper.writeValueAsString(values);
    final Values values2 = xmlMapper.readValue(valueAsString, Values.class);
    Assert.assertEquals(values2, values, valueAsString);
}