Skip to content

JSON XML Parser

landawn edited this page Apr 30, 2018 · 7 revisions

JSONParser/XMLParser is one of the core parts in AbacusUtil. It provides the high performance APIs for the general data transfer among servers/clients and conversion among different types. AbacusUtil also provides the wrappers for Jackson, Fastjson, Gson, Kryo, ObjectMapper with integrated and concise APIs. Here is a simple sample:

public void test_xmlParser() {
    Account account = createAccount();

    String str = xmlParser.serialize(account);    
    // XML: <account><id>0</id><gui>bf99db2f-9698-423c-ad7c-1668d6f03502</gui><firstName>3f2dec29-1676-4ae9-8cca-ff339d9dd5f5</firstName><lastName>2fb341dd-1e6e-4b57-ab73-ceba94ff7b65</lastName><status>0</status><lastUpdateTime>1439324554075</lastUpdateTime><createTime>1439324554075</createTime></account>

    Account account2 = xmlParser.deserialize(Account.class, str);

    assertEquals(account, account2);
}

public void test_jsonParser() {
    Account account = createAccount(N.uuid(), N.uuid());

    String str = jsonParser.serialize(account);    
    // JSON: {"id":0, "gui":"d0bc219b-b1be-4f3d-a99f-aa03001ffc62", "firstName":"15bf3775-60bb-4f52-bc8a-e95364aa4b1d", "lastName":"1253246a-f6ec-4d31-b495-3c0deb26943d", "status":0, "lastUpdateTime":1439324554283, "createTime":1439324554283}

    Account account2 = jsonParser.deserialize(Account.class, str);

    assertEquals(account, account2);
}

public void test_kryoParser() {
    Account account = createAccount(N.uuid(), N.uuid());

    String str = kryoParser.serialize(account);    
    // Encoded with Base 64: AQEAamF2YS5zcWwuVGltZXN0YW3wAcaIt/PxKQABODY2ODJjYjItYTMyNy00YjNiLTg1YzYtZDM2Y2QzNDE3MWWyATFjNTVjY2QyLWZlZDItNDliZS1hMmY0LTFlZDBkNDQzNGZltwABOTJhOWVhOWEtYTMzNi00ODNhLTkwZDctNDY1NDhjZmU0Mjm2AQABxoi38/EpAA==

    Account account2 = kryoParser.deserialize(Account.class, str);

    assertEquals(account, account2);
}

We have put a lot of efforts on performance tuning as we know it's so critical for this genernal framework. The JSON parser is about two times faster than Gson and close to Fastjson/Jackson. The XML parser is one the fastest xml parsers in the world.