JSON Support - Quiz
Total: 31 questions
1. Which approaches available for JSON support in Jersey?
Which approaches available for JSON support in Jersey?
POJO support, JAXB suppport and low-level support.
2. How to use POJO approach?
How to use POJO approach?
It is required to turn the JSONConfiguration.FEATURE_POJO_MAPPING feature on. This can be done in web.xml, setting the feature in servlet init parameter.
When using Jersey client, JSONConfiguration.FEATURE_POJO_MAPPING feature should be set to configuration.
3. A disadvantage of JAXB based approach.
A disadvantage of JAXB based approach.
A disadvantage can be appeared if it is required to work with a very specific JSON format. A lot of configuration options are provided to solve this problem.
4. The resource ExamService is accessed by GET .../exam. What is the result?
@XmlRootElement
public class Exam {
public int id;
public String name;
public Exam() {}
public Exam(int id, String name) {
this.id = id;
this.name = name;
}
}
@Path("exam")
public class ExamService {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Exam getExam() {
return new Exam(3, "OCEJWSD 6");
}
}
The resource ExamService is accessed by GET .../exam. What is the result?
@XmlRootElement
public class Exam {
public int id;
public String name;
public Exam() {}
public Exam(int id, String name) {
this.id = id;
this.name = name;
}
}
@Path("exam")
public class ExamService {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Exam getExam() {
return new Exam(3, "OCEJWSD 6");
}
}{"id":3,"name":"OCEJWSD 6"}
5. The resource ExamService is accessed by GET .../exam. What is the result?
@XmlRootElement
public class Exam {
@XmlTransient
public int id;
@XmlElement(name="certification")
public String name;
public Exam() {}
public Exam(int id, String name) {
this.id = id;
this.name = name;
}
}
@Path("exam")
public class ExamService {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Exam getExam() {
return new Exam(3, "OCEJWSD 6");
}
}
The resource ExamService is accessed by GET .../exam. What is the result?
@XmlRootElement
public class Exam {
@XmlTransient
public int id;
@XmlElement(name="certification")
public String name;
public Exam() {}
public Exam(int id, String name) {
this.id = id;
this.name = name;
}
}
@Path("exam")
public class ExamService {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Exam getExam() {
return new Exam(3, "OCEJWSD 6");
}
}{"certification":"OCEJWSD 6"}
6. Which JSON notations does JSONConfiguration support?
Which JSON notations does JSONConfiguration support?
- Mapped (default notation)
- Natural
- Jettison Mapped
- Badgerfish
7. How to set the type of JSON notation?
How to set the type of JSON notation?
Different configuration options can be set on an JSONConfiguration instance. This instance is used to create a JSONConfigurated JSONJAXBContext, which serves as a main configuration point in this area. Implement a ContextResolver to pass specialized JSONJAXBContext to Jersey.
8. How to set mapped notation to the JSONConfiguration?
How to set mapped notation to the JSONConfiguration?
JSONConfiguration.mapped().build()
9. The JSONConfiguration is set as:
JSONConfiguration.mapped().build()
The resource ExamService is accessed by GET .../exam. What is the result?
@XmlRootElement
public class Exam {
public int id;
public String name;
public List<Category> categories;
public Exam() {
}
public Exam(int id, String name, List<Category> categories) {
this.id = id;
this.name = name;
this.categories = categories;
}
}
@XmlRootElement
public class Category {
public String name;
public int questionsNumber;
public Category() {
}
public Category(String name, int questionsNumber) {
this.name = name;
this.questionsNumber = questionsNumber;
}
}
@Path("exam")
public class ExamService {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Exam getExam() {
List<Category> categories = new ArrayList<Category>();
categories.add(new Category("JAXP",3));
return new Exam(3, "OCEJWSD 6", categories);
}
}
The JSONConfiguration is set as:
JSONConfiguration.mapped().build()
The resource ExamService is accessed by GET .../exam. What is the result?
@XmlRootElement
public class Exam {
public int id;
public String name;
public List<Category> categories;
public Exam() {
}
public Exam(int id, String name, List<Category> categories) {
this.id = id;
this.name = name;
this.categories = categories;
}
}
@XmlRootElement
public class Category {
public String name;
public int questionsNumber;
public Category() {
}
public Category(String name, int questionsNumber) {
this.name = name;
this.questionsNumber = questionsNumber;
}
}
@Path("exam")
public class ExamService {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Exam getExam() {
List<Category> categories = new ArrayList<Category>();
categories.add(new Category("JAXP",3));
return new Exam(3, "OCEJWSD 6", categories);
}
}{"id":"3","name":"OCEJWSD 6","categories":{"name":"JAXP","questionsNumber":"3"}}
10. How to instruct the JSON processor, what items need to be treated as arrays in mapped notation?
How to instruct the JSON processor, what items need to be treated as arrays in mapped notation?
By setting an optional property, arrays, on your JSONConfiguration object:
JSONConfiguration.mapped().arrays("categories").build()
11. How to instruct the JSON processor, what items need to be treated as non-string values in mapped JSON notation?
How to instruct the JSON processor, what items need to be treated as non-string values in mapped JSON notation?
JSONConfiguration.mapped().nonStrings("id","questionsNumber").build()
12. The JSONConfiguration is set as:
JSONConfiguration.mapped().arrays("categories").nonStrings("id","questionsNumber").build()
The resource ExamService is accessed by GET .../exam. What is the result?
@XmlRootElement
public class Exam {
public int id;
public String name;
public List<Category> categories;
public Exam() {
}
public Exam(int id, String name, List<Category> categories) {
this.id = id;
this.name = name;
this.categories = categories;
}
}
@XmlRootElement
public class Category {
public String name;
public int questionsNumber;
public Category() {
}
public Category(String name, int questionsNumber) {
this.name = name;
this.questionsNumber = questionsNumber;
}
}
@Path("exam")
public class ExamService {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Exam getExam() {
List<Category> categories = new ArrayList<Category>();
categories.add(new Category("JAXP",3));
return new Exam(3, "OCEJWSD 6", categories);
}
}
The JSONConfiguration is set as:
JSONConfiguration.mapped().arrays("categories").nonStrings("id","questionsNumber").build()
The resource ExamService is accessed by GET .../exam. What is the result?
@XmlRootElement
public class Exam {
public int id;
public String name;
public List<Category> categories;
public Exam() {
}
public Exam(int id, String name, List<Category> categories) {
this.id = id;
this.name = name;
this.categories = categories;
}
}
@XmlRootElement
public class Category {
public String name;
public int questionsNumber;
public Category() {
}
public Category(String name, int questionsNumber) {
this.name = name;
this.questionsNumber = questionsNumber;
}
}
@Path("exam")
public class ExamService {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Exam getExam() {
List<Category> categories = new ArrayList<Category>();
categories.add(new Category("JAXP",3));
return new Exam(3, "OCEJWSD 6", categories);
}
}{"id":3,"name":"OCEJWSD 6","categories":[{"name":"JAXP","questionsNumber":3}]}
13. What is the serialized JSON result for:
@XmlRootElement
public class Exam {
@XmlAttribute
public int id;
...
}
What is the serialized JSON result for:
@XmlRootElement
public class Exam {
@XmlAttribute
public int id;
...
}{"@id":"3"...
14. How to keep a XML root tag equivalent generated in your JSON on mapped notation?
How to keep a XML root tag equivalent generated in your JSON on mapped notation?
Use rootUnwrapping configuration option:
JSONConfiguration.mapped().rootUnwrapping(false).build()
15. The JSONConfiguration is set as:
JSONConfiguration.mapped().rootUnwrapping(false).build()
The resource ExamService is accessed by GET .../exam. What is the result?
@XmlRootElement
public class Exam {
public int id;
public String name;
public List<Category> categories;
public Exam() {
}
public Exam(int id, String name, List<Category> categories) {
this.id = id;
this.name = name;
this.categories = categories;
}
}
@XmlRootElement
public class Category {
public String name;
public int questionsNumber;
public Category() {
}
public Category(String name, int questionsNumber) {
this.name = name;
this.questionsNumber = questionsNumber;
}
}
@Path("exam")
public class ExamService {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Exam getExam() {
List<Category> categories = new ArrayList<Category>();
categories.add(new Category("JAXP",3));
return new Exam(3, "OCEJWSD 6", categories);
}
}
The JSONConfiguration is set as:
JSONConfiguration.mapped().rootUnwrapping(false).build()
The resource ExamService is accessed by GET .../exam. What is the result?
@XmlRootElement
public class Exam {
public int id;
public String name;
public List<Category> categories;
public Exam() {
}
public Exam(int id, String name, List<Category> categories) {
this.id = id;
this.name = name;
this.categories = categories;
}
}
@XmlRootElement
public class Category {
public String name;
public int questionsNumber;
public Category() {
}
public Category(String name, int questionsNumber) {
this.name = name;
this.questionsNumber = questionsNumber;
}
}
@Path("exam")
public class ExamService {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Exam getExam() {
List<Category> categories = new ArrayList<Category>();
categories.add(new Category("JAXP",3));
return new Exam(3, "OCEJWSD 6", categories);
}
}{"exam":{"id":"3","name":"OCEJWSD 6","categories":{"name":"JAXP","questionsNumber":"3"}}}
16. What is the serialized JSON result for:
@XmlRootElement
public class Exam {
@XmlElement(namespace="http://examclouds.com")
public int id;
...
}
If the JSONConfiguration is set as:
Map<String,String> ns2json = new HashMap<String, String>();
ns2json.put("http://examclouds.com", "examclouds");
this.context = new JSONJAXBContext(
JSONConfiguration.mapped().xml2JsonNs(ns2json).build(), types);
What is the serialized JSON result for:
@XmlRootElement
public class Exam {
@XmlElement(namespace="http://examclouds.com")
public int id;
...
}
If the JSONConfiguration is set as:
Map<String,String> ns2json = new HashMap<String, String>();
ns2json.put("http://examclouds.com", "examclouds");
this.context = new JSONJAXBContext(
JSONConfiguration.mapped().xml2JsonNs(ns2json).build(), types);{"examclouds.id":"3",...
17. The advantage of natural JSON notation?
The advantage of natural JSON notation?
With natural notation, Jersey will automatically figure out how individual items need to be processed, so that you do not need to do any kind of manual configuration. Java arrays and lists are mapped into JSON arrays, even for single-element cases. Java numbers and booleans are correctly mapped into JSON numbers and booleans, and you do not need to bother with XML attributes, as in JSON, they keep the original names.
18. How to set natural notation to the JSONConfiguration?
How to set natural notation to the JSONConfiguration?
JSONConfiguration.natural().build()
19. The JSONConfiguration is set as:
JSONConfiguration.natural().build()
The resource ExamService is accessed by GET .../exam. What is the result?
@XmlRootElement
public class Exam {
public int id;
public String name;
public List<Category> categories;
public Exam() {
}
public Exam(int id, String name, List<Category> categories) {
this.id = id;
this.name = name;
this.categories = categories;
}
}
@XmlRootElement
public class Category {
public String name;
public int questionsNumber;
public Category() {
}
public Category(String name, int questionsNumber) {
this.name = name;
this.questionsNumber = questionsNumber;
}
}
@Path("exam")
public class ExamService {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Exam getExam() {
List<Category> categories = new ArrayList<Category>();
categories.add(new Category("JAXP",3));
return new Exam(3, "OCEJWSD 6", categories);
}
}
The JSONConfiguration is set as:
JSONConfiguration.natural().build()
The resource ExamService is accessed by GET .../exam. What is the result?
@XmlRootElement
public class Exam {
public int id;
public String name;
public List<Category> categories;
public Exam() {
}
public Exam(int id, String name, List<Category> categories) {
this.id = id;
this.name = name;
this.categories = categories;
}
}
@XmlRootElement
public class Category {
public String name;
public int questionsNumber;
public Category() {
}
public Category(String name, int questionsNumber) {
this.name = name;
this.questionsNumber = questionsNumber;
}
}
@Path("exam")
public class ExamService {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Exam getExam() {
List<Category> categories = new ArrayList<Category>();
categories.add(new Category("JAXP",3));
return new Exam(3, "OCEJWSD 6", categories);
}
}{"id":3,"name":"OCEJWSD 6","categories":[{"name":"JAXP","questionsNumber":3}]}
20. How to keep XML root tag equivalent in JSON natural JSON notation?
How to keep XML root tag equivalent in JSON natural JSON notation?
JSONConfiguration.natural().rootUnwrapping(false).build()
21. How to set jettison mapped notation to the JSONConfiguration?
How to set jettison mapped notation to the JSONConfiguration?
JSONConfiguration.mappedJettison().build()
22. When Jettison mapped notation can be used?
When Jettison mapped notation can be used?
It can be used when working with more complex XML documents for example with multiple XML namespaces in your JAXB beans.
23. The JSONConfiguration is set as:
JSONConfiguration.mappedJettison().build()
The resource ExamService is accessed by GET .../exam. What is the result?
@XmlRootElement
public class Exam {
public int id;
public String name;
public List<Category> categories;
public Exam() {
}
public Exam(int id, String name, List<Category> categories) {
this.id = id;
this.name = name;
this.categories = categories;
}
}
@XmlRootElement
public class Category {
public String name;
public int questionsNumber;
public Category() {
}
public Category(String name, int questionsNumber) {
this.name = name;
this.questionsNumber = questionsNumber;
}
}
@Path("exam")
public class ExamService {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Exam getExam() {
List<Category> categories = new ArrayList<Category>();
categories.add(new Category("JAXP",3));
return new Exam(3, "OCEJWSD 6", categories);
}
}
The JSONConfiguration is set as:
JSONConfiguration.mappedJettison().build()
The resource ExamService is accessed by GET .../exam. What is the result?
@XmlRootElement
public class Exam {
public int id;
public String name;
public List<Category> categories;
public Exam() {
}
public Exam(int id, String name, List<Category> categories) {
this.id = id;
this.name = name;
this.categories = categories;
}
}
@XmlRootElement
public class Category {
public String name;
public int questionsNumber;
public Category() {
}
public Category(String name, int questionsNumber) {
this.name = name;
this.questionsNumber = questionsNumber;
}
}
@Path("exam")
public class ExamService {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Exam getExam() {
List<Category> categories = new ArrayList<Category>();
categories.add(new Category("JAXP",3));
return new Exam(3, "OCEJWSD 6", categories);
}
}{"exam":{"id":3,"name":"OCEJWSD 6","categories":{"name":"JAXP","questionsNumber":3}}}
24. The JSONConfiguration is set as:
Map<String,String> ns2json = new HashMap<String, String>();
ns2json.put("http://www.examclouds.com", "examclouds");
this.context = new JSONJAXBContext(
JSONConfiguration.mappedJettison().xml2JsonNs(ns2json).build(), types);
What is the serialized JSON result for:
@XmlRootElement
public class Exam {
@XmlElement(namespace="http://www.examclouds.com")
public int id;
...
}
The JSONConfiguration is set as:
Map<String,String> ns2json = new HashMap<String, String>();
ns2json.put("http://www.examclouds.com", "examclouds");
this.context = new JSONJAXBContext(
JSONConfiguration.mappedJettison().xml2JsonNs(ns2json).build(), types);
What is the serialized JSON result for:
@XmlRootElement
public class Exam {
@XmlElement(namespace="http://www.examclouds.com")
public int id;
...
}{"exam":{"examclouds.id":3,
"name":"OCEJWSD 6",
"categories":{"name":"JAXP","questionsNumber":3}}}
25. When badgerfish notation can be used?
When badgerfish notation can be used?
If you need to make sure your JAXB beans could be flawlessly written and read back to and from JSON, without bothering with any formatting configuration, namespaces, etc.
26. How to set badgerfish notation to the JSONConfiguration?
How to set badgerfish notation to the JSONConfiguration?
JSONConfiguration.badgerFish().build()
27. The JSONConfiguration is set as:
JSONConfiguration.badgerFish().build()
The resource ExamService is accessed by GET .../exam. What is the result?
@XmlRootElement
public class Exam {
public int id;
public String name;
public List<Category> categories;
public Exam() {
}
public Exam(int id, String name, List<Category> categories) {
this.id = id;
this.name = name;
this.categories = categories;
}
}
@XmlRootElement
public class Category {
public String name;
public int questionsNumber;
public Category() {
}
public Category(String name, int questionsNumber) {
this.name = name;
this.questionsNumber = questionsNumber;
}
}
@Path("exam")
public class ExamService {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Exam getExam() {
List<Category> categories = new ArrayList<Category>();
categories.add(new Category("JAXP",3));
return new Exam(3, "OCEJWSD 6", categories);
}
}
The JSONConfiguration is set as:
JSONConfiguration.badgerFish().build()
The resource ExamService is accessed by GET .../exam. What is the result?
@XmlRootElement
public class Exam {
public int id;
public String name;
public List<Category> categories;
public Exam() {
}
public Exam(int id, String name, List<Category> categories) {
this.id = id;
this.name = name;
this.categories = categories;
}
}
@XmlRootElement
public class Category {
public String name;
public int questionsNumber;
public Category() {
}
public Category(String name, int questionsNumber) {
this.name = name;
this.questionsNumber = questionsNumber;
}
}
@Path("exam")
public class ExamService {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Exam getExam() {
List<Category> categories = new ArrayList<Category>();
categories.add(new Category("JAXP",3));
return new Exam(3, "OCEJWSD 6", categories);
}
}{"exam":{"id":{"$":"3"},
"name":{"$":"OCEJWSD 6"},
"categories":{"name":{"$":"JAXP"},"questionsNumber":{"$":"3"}}}}
28. How to implement low-level JSON support?
How to implement low-level JSON support?
JSONObject and JSONArray classes taken from Jettison project are used for data representations.
29. The advantage of low-level JSON support?
The advantage of low-level JSON support?
You will gain full control over the JSON format produced and consumed.
30. The disadvantage of low-level JSON support?
The disadvantage of low-level JSON support?
Dealing with your data model objects will probably be a bit more complex, than when taking the JAXB based approach.
31. Construct an equivalent JSONObject:
List<Category> categories = new ArrayList<Category>();
categories.add(new Category("JAXP",3));
Exam exam = Exam(3, "OCEJWSD 6", categories);
Construct an equivalent JSONObject:
List<Category> categories = new ArrayList<Category>();
categories.add(new Category("JAXP",3));
Exam exam = Exam(3, "OCEJWSD 6", categories);JSONObject exam = new JSONObject();
JSONObject category = new JSONObject();
JSONArray categories = new JSONArray();
try {
category.put("name", "JAXP");
category.put("questionsNumber", 3);
categories.put(category);
exam.put("id", 3);
exam.put("name", "OCEJWSD 6");
exam.put("categories", categories);
} catch (JSONException e) {
System.out.print(e);
}