Skip to content

Commit

Permalink
Add json Hello World
Browse files Browse the repository at this point in the history
  • Loading branch information
vedenin committed Apr 21, 2016
1 parent 9601da7 commit e2865cb
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 33 deletions.
Expand Up @@ -3,32 +3,32 @@
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;


/** /**
* FastJson Hello World * FastJson Hello Place
* *
*/ */
public class FastJsonHelloWorld { public class FastJsonHelloWorld {


public static void main(String[] args) { public static void main(String[] args) {
// init class // init class
World world = new World(); Place place = new Place();
world.setName("World"); place.setName("World");


Human human = new Human(); Human human = new Human();
human.setMessage("Hi"); human.setMessage("Hi");
human.setWorld(world); human.setPlace(place);


// convert to json // convert to json
String jsonString = JSON.toJSONString(human); String jsonString = JSON.toJSONString(human);
System.out.println("json " + jsonString); System.out.println("json " + jsonString); // print "json {"message":"Hi","place":{"name":"World"}}"


// convert from json // convert from json
Human newHuman = JSON.parseObject(jsonString, Human.class); Human newHuman = JSON.parseObject(jsonString, Human.class);
newHuman.say(); newHuman.say(); // print "Hi , World!"
} }


private static class Human { private static class Human {
private String message; private String message;
private World world; private Place place;


public String getMessage() { public String getMessage() {
return message; return message;
Expand All @@ -38,21 +38,21 @@ public void setMessage(String message) {
this.message = message; this.message = message;
} }


public World getWorld() { public Place getPlace() {
return world; return place;
} }


public void setWorld(World world) { public void setPlace(Place place) {
this.world = world; this.place = place;
} }


public void say() { public void say() {
System.out.println(); System.out.println();
System.out.println(getMessage() + " , " + getWorld().getName() + "!"); System.out.println(getMessage() + " , " + getPlace().getName() + "!");
} }
} }


private static class World { private static class Place {
private String name; private String name;


public String getName() { public String getName() {
Expand Down
Expand Up @@ -6,66 +6,66 @@
import java.io.IOException; import java.io.IOException;


/** /**
* Genson Hello World * Genson Hello Place
* *
*/ */
public class GensonHelloWorld { public class GensonHelloWorld {


public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
// init class // init class
World world = new World(); Place place = new Place();
world.setName("World"); place.setName("World");


Human human = new Human(); Human human = new Human();
human.setMessage("Hi"); human.setMessage("Hi");
human.setWorld(world); human.setPlace(place);


// convert to json // convert to json
Moshi moshi = new Moshi.Builder().build(); Moshi moshi = new Moshi.Builder().build();
JsonAdapter<Human> jsonAdapter = moshi.adapter(Human.class); JsonAdapter<Human> jsonAdapter = moshi.adapter(Human.class);


String jsonString = jsonAdapter.toJson(human); String jsonString = jsonAdapter.toJson(human);
System.out.println("json " + jsonString); System.out.println("json " + jsonString); //print "json {"message":"Hi","place":{"name":"World"}}"


// convert from json // convert from json
Human newHuman = jsonAdapter.fromJson(jsonString); Human newHuman = jsonAdapter.fromJson(jsonString);
newHuman.say(); newHuman.say(); // print "Hi , World!"
} }


private static class Human { private static class Human {
private String message; private String message;
private World world; private Place place;


public String getMessage() { String getMessage() {
return message; return message;
} }


public void setMessage(String message) { void setMessage(String message) {
this.message = message; this.message = message;
} }


public World getWorld() { Place getPlace() {
return world; return place;
} }


public void setWorld(World world) { void setPlace(Place place) {
this.world = world; this.place = place;
} }


public void say() { void say() {
System.out.println(); System.out.println();
System.out.println(getMessage() + " , " + getWorld().getName() + "!"); System.out.println(getMessage() + " , " + getPlace().getName() + "!");
} }
} }


private static class World { private static class Place {
private String name; private String name;


public String getName() { String getName() {
return name; return name;
} }


public void setName(String name) { void setName(String name) {
this.name = name; this.name = name;
} }
} }
Expand Down
6 changes: 5 additions & 1 deletion helloworlds/3.8-json/ig_json_parser/pom.xml
Expand Up @@ -9,6 +9,10 @@
<version>0.01</version> <version>0.01</version>


<dependencies> <dependencies>

<dependency>
<groupId>com.instagram</groupId>
<artifactId>ig-json-parser-processor</artifactId>
<version>0.0.6</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
@@ -0,0 +1,22 @@
package ig_json_parser;

import com.instagram.common.json.annotation.JsonField;
import com.instagram.common.json.annotation.JsonType;

/**
* Created by vedenin on 22.04.16.
*/
@JsonType
public class Human {
@JsonField(fieldName="message")
public String message;
@JsonField(fieldName="place")
public Place place;



public void say() {
System.out.println();
System.out.println(message + " , " + place.name + "!");
}
}
@@ -1,12 +1,29 @@
package ig_json_parser; package ig_json_parser;


import com.instagram.common.json.annotation.JsonField;
import com.instagram.common.json.annotation.JsonType;

/** /**
* Ig Json Parser Hello World * Ig Json Parser Hello World
* *
*/ */
public class IgJsonParserHelloWorld { public class IgJsonParserHelloWorld {


public static void main(String[] args) { public static void main(String[] args) throws Exception {
// init class
Place place = new Place();
place.name = "World";

Human human = new Human();
human.message = "Hi";
human.place = place;

// convert to json
String jsonString = Human__JsonHelper.serializeToJson(human);
System.out.println("json " + jsonString); //print "json {"place":{"name":"World"},"message":"Hi"}"


// convert from json
Human newHuman = Human__JsonHelper.parseFromJson(jsonString);
newHuman.say(); // print "Hi , World!"
} }
} }
@@ -0,0 +1,13 @@
package ig_json_parser;

import com.instagram.common.json.annotation.JsonField;
import com.instagram.common.json.annotation.JsonType;

/**
* Created by vedenin on 22.04.16.
*/
@JsonType
public class Place {
@JsonField(fieldName="name")
public String name;
}

0 comments on commit e2865cb

Please sign in to comment.