11package com .library .app .book .resource ;
22
3- import javax .enterprise .context .ApplicationScoped ;
4- import javax .inject .Inject ;
5-
63import com .google .gson .JsonArray ;
74import com .google .gson .JsonElement ;
85import com .google .gson .JsonObject ;
1411import com .library .app .common .json .EntityJsonConverter ;
1512import com .library .app .common .json .JsonReader ;
1613
14+ import javax .enterprise .context .ApplicationScoped ;
15+ import javax .inject .Inject ;
16+
1717@ ApplicationScoped
1818public class BookJsonConverter implements EntityJsonConverter <Book > {
1919
20- @ Inject
21- CategoryJsonConverter categoryJsonConverter ;
20+ @ Inject
21+ CategoryJsonConverter categoryJsonConverter ;
2222
23- @ Inject
24- AuthorJsonConverter authorJsonConverter ;
23+ @ Inject
24+ AuthorJsonConverter authorJsonConverter ;
2525
26- @ Override
27- public Book convertFrom (final String json ) {
28- final JsonObject jsonObject = JsonReader .readAsJsonObject (json );
26+ @ Override
27+ public Book convertFrom (final String json ) {
28+ final JsonObject jsonObject = JsonReader .readAsJsonObject (json );
2929
30- final Book book = new Book ();
31- book .setTitle (JsonReader .getStringOrNull (jsonObject , "title" ));
32- book .setDescription (JsonReader .getStringOrNull (jsonObject , "description" ));
30+ final Book book = new Book ();
31+ book .setTitle (JsonReader .getStringOrNull (jsonObject , "title" ));
32+ book .setDescription (JsonReader .getStringOrNull (jsonObject , "description" ));
3333
34- final Category category = new Category ();
35- category .setId (JsonReader .getLongOrNull (jsonObject , "categoryId" ));
36- book .setCategory (category );
34+ final Category category = new Category ();
35+ category .setId (JsonReader .getLongOrNull (jsonObject , "categoryId" ));
36+ book .setCategory (category );
3737
38- final JsonArray authorsIdsJsonArray = jsonObject .getAsJsonArray ("authorsIds" );
39- if (authorsIdsJsonArray != null ) {
40- for (final JsonElement authorIdJsonElement : authorsIdsJsonArray ) {
41- final Author author = new Author ();
42- author .setId (authorIdJsonElement .getAsLong ());
43- book .addAuthor (author );
44- }
45- }
38+ final JsonArray authorsIdsJsonArray = jsonObject .getAsJsonArray ("authorsIds" );
39+ if (authorsIdsJsonArray != null ) {
40+ for (final JsonElement authorIdJsonElement : authorsIdsJsonArray ) {
41+ final Author author = new Author ();
42+ author .setId (authorIdJsonElement .getAsLong ());
43+ book .addAuthor (author );
44+ }
45+ }
4646
47- book .setPrice (JsonReader .getDoubeOrNull (jsonObject , "price" ));
47+ book .setPrice (JsonReader .getDoubeOrNull (jsonObject , "price" ));
4848
49- return book ;
50- }
49+ return book ;
50+ }
5151
52- @ Override
53- public JsonElement convertToJsonElement (final Book book ) {
54- final JsonObject jsonObject = new JsonObject ();
52+ @ Override
53+ public JsonElement convertToJsonElement (final Book book ) {
54+ final JsonObject jsonObject = new JsonObject ();
5555
56- jsonObject .addProperty ("id" , book .getId ());
57- jsonObject .addProperty ("title" , book .getTitle ());
58- jsonObject .addProperty ("description" , book .getDescription ());
59- jsonObject .add ("category" , categoryJsonConverter .convertToJsonElement (book .getCategory ()));
60- jsonObject .add ("authors" , authorJsonConverter .convertToJsonElement (book .getAuthors ()));
61- jsonObject .addProperty ("price" , book .getPrice ());
56+ jsonObject .addProperty ("id" , book .getId ());
57+ jsonObject .addProperty ("title" , book .getTitle ());
58+ jsonObject .addProperty ("description" , book .getDescription ());
59+ jsonObject .add ("category" , categoryJsonConverter .convertToJsonElement (book .getCategory ()));
60+ jsonObject .add ("authors" , authorJsonConverter .convertToJsonElement (book .getAuthors ()));
61+ jsonObject .addProperty ("price" , book .getPrice ());
6262
63- return jsonObject ;
64- }
63+ return jsonObject ;
64+ }
6565}
0 commit comments