001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020package org.apache.juneau.examples.core.json; 021 022import org.apache.juneau.examples.core.pojo.Pojo; 023import org.apache.juneau.json.JsonParser; 024import org.apache.juneau.json.JsonSerializer; 025import org.apache.juneau.json.SimpleJsonSerializer; 026 027import java.util.Map; 028 029/** 030 * Sample class which shows the simple usage of JsonSerializer and JsonParser. 031 */ 032public class JsonSimpleExample { 033 034 /** 035 * Serializing Pojo bean into Json format 036 * and Deserialize back to Pojo instance type. 037 * @param args 038 * @throws Exception 039 */ 040 @SuppressWarnings({ "unused", "rawtypes" }) 041 public static void main(String[] args) throws Exception{ 042 // Juneau provides static constants with the most commonly used configurations 043 // Get a reference to a serializer - converting POJO to flat format 044 // Produces 045 // {"name":"name","id":"id"} 046 JsonSerializer jsonSerializer = JsonSerializer.DEFAULT; 047 // Get a reference to a parser - converts that flat format back into the POJO 048 JsonParser jsonParser = JsonParser.DEFAULT; 049 050 Pojo pojo = new Pojo("id","name"); 051 052 String flat = jsonSerializer.serialize(pojo); 053 // Print out the created POJO in JSON format. 054 System.out.println(flat); 055 056 Pojo parse = jsonParser.parse(flat, Pojo.class); 057 058 assert parse.getId().equals(pojo.getId()); 059 assert parse.getName().equals(pojo.getName()); 060 061 // Produces 062 // {name:'name',id:'id'} 063 String simpleJson = SimpleJsonSerializer.DEFAULT.serialize(pojo); 064 System.out.println(simpleJson); 065 066 // Parse a JSON object (creates a generic ObjectMap). 067 String json = "{name:'John Smith',age:21}"; 068 Map m1 = jsonParser.parse(json, Map.class); 069 070 // Parse a JSON string. 071 json = "'foobar'"; 072 String s2 = jsonParser.parse(json, String.class); 073 074 // Parse a JSON number as a Long or Float. 075 json = "123"; 076 Long l3 = jsonParser.parse(json, Long.class); 077 Float f3 = jsonParser.parse(json, Float.class); 078 079 // The object above can be parsed thanks to the @BeanConstructor(properties = id,name) annotation on Pojo 080 // Using this approach, you can keep your POJOs immutable, and still serialize and deserialize them. 081 } 082}