List of usage examples for com.google.gson FieldAttributes getName
public String getName()
From source file:meta.FieldExcluderForGson.java
License:Open Source License
@Override public boolean shouldSkipField(FieldAttributes f) { return (f.getName().equals("id") || f.getName().startsWith("_") || f.getAnnotation(ManyToOne.class) != null); }
From source file:nars.io.JSONOutput.java
License:Open Source License
protected void init(boolean pretty) { GsonBuilder builder = new GsonBuilder().addSerializationExclusionStrategy(new ExclusionStrategy() { @Override//from w w w . ja v a2s . co m public boolean shouldSkipField(FieldAttributes fa) { //System.out.println(fa.getDeclaredClass() + //" " + fa.getName()); if (fa.getName().equals("derivationChain")) { return true; } return false; } @Override public boolean shouldSkipClass(Class<?> type) { return false; } }) //.registerTypeAdapter(Id.class, new IdTypeAdapter()) //.setDateFormat(DateFormat.LONG) //.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE) //.setVersion(1.0) .enableComplexMapKeySerialization().serializeNulls().disableHtmlEscaping(); if (pretty) { builder.setPrettyPrinting(); } gson = builder.create(); }
From source file:nars.util.JSONOutput.java
License:Open Source License
protected void init(boolean pretty) { GsonBuilder builder = new GsonBuilder().addSerializationExclusionStrategy(new ExclusionStrategy() { @Override/* ww w. j a v a 2 s.c om*/ public boolean shouldSkipField(FieldAttributes fa) { return fa.getName().equals("derivationChain"); } @Override public boolean shouldSkipClass(Class<?> type) { return false; } }) //.registerTypeAdapter(Id.class, new IdTypeAdapter()) //.setDateFormat(DateFormat.LONG) //.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE) //.setVersion(1.0) .enableComplexMapKeySerialization().serializeNulls().disableHtmlEscaping(); if (pretty) { builder.setPrettyPrinting(); } gson = builder.create(); }
From source file:net.es.enos.services.gson.JsonExclusionStrategy.java
License:Open Source License
@Override public boolean shouldSkipField(FieldAttributes f) { return "self".contentEquals(f.getName()); }
From source file:nl.binaryimpact.showcase.json.GeneratedIdExclusionStrategy.java
License:Apache License
/** * Always skips fields with field name <code>"_id"</code>. These fields are reserved for the local database. * * @param f Describes the field to be tested for exclusion. * @return True if the field name equals <code>"_id"</code>. *///from www . ja v a2s. c o m @Override public boolean shouldSkipField(FieldAttributes f) { return "_id".equals(f.getName()); }
From source file:ordo.servlet.api.APILieuxController.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.//from w ww . java2s .c om * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/json"); JpaDepotDao daoDepot = JpaDepotDao.getInstance(); JpaSwapLocationDao daoSwapLocation = JpaSwapLocationDao.getInstance(); JpaCommandeClientDao daoCommandeClient = JpaCommandeClientDao.getInstance(); Collection<Lieu> lieux = new ArrayList<>(); lieux.addAll(daoDepot.findAll()); lieux.addAll(daoSwapLocation.findAll()); lieux.addAll(daoCommandeClient.findAll(false)); if (lieux.isEmpty()) { CommandeClient c1 = new CommandeClient(); Depot c2 = new Depot(); c1.setId(1); c1.setQuantiteVoulue(26); c1.setCoordY(50.4291629f); c1.setCoordX(2.8278697f); c1.setLibelle("Sample Lieu"); c2.setId(2); c2.setCoordY(50.5f); c2.setCoordX(2.8278697f); lieux.add(c1); lieux.add(c2); } String json = new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() { @Override public boolean shouldSkipField(FieldAttributes f) { switch (f.getName()) { case "vehicule": case "colis": return true; } return false; } @Override public boolean shouldSkipClass(Class<?> clazz) { return false; } }).create().toJson(lieux); json = "{\"lieux\": " + json + "}"; PrintWriter out = response.getWriter(); out.print(json); }
From source file:org.dspace.app.webui.discovery.DiscoveryJSONRequest.java
License:BSD License
private void doAutocomplete(Context context, HttpServletRequest request, HttpServletResponse resp) { try {/*from w w w .ja v a2s. co m*/ DSpaceObject scope = DiscoverUtility.getSearchScope(context, request); DiscoverQuery autocompleteQuery = DiscoverUtility.getDiscoverAutocomplete(context, request, scope); DiscoverResult qResults = SearchUtils.getSearchService().search(context, autocompleteQuery); // extract the only facet present in the result response Set<String> facets = qResults.getFacetResults().keySet(); List<FacetResult> fResults = new ArrayList<DiscoverResult.FacetResult>(); if (facets != null && facets.size() > 0) { String autocompleteField = (String) facets.toArray()[0]; fResults = qResults.getFacetResult(autocompleteField); } Gson gson = new GsonBuilder().addSerializationExclusionStrategy(new ExclusionStrategy() { @Override public boolean shouldSkipField(FieldAttributes f) { if (f.getName().equals("asFilterQuery")) return true; return false; } @Override public boolean shouldSkipClass(Class<?> clazz) { return false; } }).create(); JsonElement tree = gson.toJsonTree(fResults); JsonObject jo = new JsonObject(); jo.add("autocomplete", tree); resp.getWriter().write(jo.toString()); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } }
From source file:org.eclipse.epp.internal.logging.aeri.ui.utils.EmfFieldExclusionStrategy.java
License:Open Source License
@Override public boolean shouldSkipField(FieldAttributes f) { String name = f.getName(); if (name.length() > 1 && name.charAt(0) == 'e' && Character.isUpperCase(name.charAt(1))) { return true; }// w w w .j a v a 2s . c om return false; }
From source file:org.eclipse.mylyn.internal.gerrit.core.client.JSonSupport.java
License:Open Source License
public JSonSupport() { TypeToken<Map<Id, PatchSetApproval>> approvalMapType = new TypeToken<Map<ApprovalCategory.Id, PatchSetApproval>>() { };/*from w w w . java2 s .c o m*/ ExclusionStrategy exclustionStrategy = new ExclusionStrategy() { public boolean shouldSkipField(FieldAttributes f) { // commentLinks requires instantiation of com.google.gwtexpui.safehtml.client.RegexFindReplace which is not on classpath if (f.getDeclaredClass() == List.class && f.getName().equals("commentLinks") //$NON-NLS-1$ && f.getDeclaringClass() == GerritConfig.class) { return true; } if (f.getDeclaredClass() == Map.class && f.getName().equals("given")) { //$NON-NLS-1$ //return true; } // GSon 2.1 fails to deserialize the SubmitType enum if (f.getDeclaringClass() == Project.class && f.getName().equals("submitType")) { //$NON-NLS-1$ return true; } return false; } public boolean shouldSkipClass(Class<?> clazz) { return false; } }; gson = JsonServlet.defaultGsonBuilder() .registerTypeAdapter(JSonResponse.class, new JSonResponseDeserializer()) .registerTypeAdapter(Edit.class, new JsonDeserializer<Edit>() { public Edit deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonArray()) { JsonArray array = json.getAsJsonArray(); if (array.size() == 4) { return new Edit(array.get(0).getAsInt(), array.get(1).getAsInt(), array.get(2).getAsInt(), array.get(3).getAsInt()); } } return new Edit(0, 0); } }) // ignore GerritForge specific AuthType "TEAMFORGE" which is unknown to Gerrit .registerTypeAdapter(AuthType.class, new JsonDeserializer<AuthType>() { @Override public AuthType deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { String jsonString = json.getAsString(); if (jsonString != null) { try { return AuthType.valueOf(jsonString); } catch (IllegalArgumentException e) { // ignore the error since the connector does not make use of AuthType //GerritCorePlugin.logWarning("Ignoring unkown authentication type: " + jsonString, e); } } return null; } }) .registerTypeAdapter(approvalMapType.getType(), new JsonDeserializer<Map<Id, PatchSetApproval>>() { @Override public Map<Id, PatchSetApproval> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { // Gerrit 2.2: the type of PatchSetPublishDetail.given changed from a map to a list Map<Id, PatchSetApproval> map = new HashMap<ApprovalCategory.Id, PatchSetApproval>(); if (json.isJsonArray()) { JsonArray array = json.getAsJsonArray(); for (Iterator<JsonElement> it = array.iterator(); it.hasNext();) { JsonElement element = it.next(); Id key = context.deserialize(element, Id.class); if (key.get() != null) { // Gerrit < 2.1.x: json is map element = it.next(); } PatchSetApproval value = context.deserialize(element, PatchSetApproval.class); if (key.get() == null) { // Gerrit 2.2: json is a list, deduct key from value key = value.getCategoryId(); } map.put(key, value); } } return map; } }).setExclusionStrategies(exclustionStrategy).create(); }
From source file:org.fao.geonet.api.FieldNameExclusionStrategy.java
License:Open Source License
@Override public boolean shouldSkipField(FieldAttributes fieldAttributes) { return excludedFields.indexOf(fieldAttributes.getName()) != -1; }