View Javadoc
1   package com.exsoinn.util.epf;
2   
3   import com.google.gson.JsonArray;
4   import com.google.gson.JsonElement;
5   import com.google.gson.JsonObject;
6   
7   /**
8    * Created by QuijadaJ on 5/24/2017.
9    */
10  public class MutableJsonContext extends JsonContext implements MutableContext {
11      MutableJsonContext(JsonElement pJsonElement) {
12          super(pJsonElement);
13      }
14  
15      @Override
16      public void addMember(String pName, Context pContext) {
17          if (!isRecursible()) {
18              throw new IllegalStateException("This operation not supported for elements that are not complex.");
19          }
20  
21          // Need the underlying JSON elements of both sender and receiving objects in order
22          // to be able to invoke GSON's API to add to it
23          JsonElement srcJson = ((JsonContext) pContext).unwrap();
24          JsonObject targetJson = (JsonObject) unwrap();
25          targetJson.add(pName, srcJson);
26      }
27  
28      @Override
29      public void addEntryToArray(Context pEntry) {
30          if (!isArray()) {
31              throw new IllegalStateException("This operation not supported for elements that are not array.");
32          }
33  
34          JsonElement srcJson = ((JsonContext) pEntry).unwrap();
35          JsonArray targetJson = (JsonArray) unwrap();
36          targetJson.add(srcJson);
37      }
38  }