JsonDeserializer.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.utils.rest.problem;

  21. import java.util.Iterator;

  22. import org.openspcoop2.utils.UtilsException;
  23. import org.openspcoop2.utils.json.JSONUtils;

  24. import com.fasterxml.jackson.databind.node.ObjectNode;

  25. /**
  26.  * JsonDeserializer
  27.  *
  28.  * @author Poli Andrea (apoli@link.it)
  29.  * @author $Author$
  30.  * @version $Rev$, $Date$
  31.  */
  32. public class JsonDeserializer extends AbstractDeserializer {

  33.     private JSONUtils jsonUtils;
  34.    
  35.     public JsonDeserializer() {
  36.         this(false);
  37.     }
  38.     public JsonDeserializer(boolean generateTypeBlank) {
  39.         super(generateTypeBlank);
  40.         this.jsonUtils = JSONUtils.getInstance();
  41.     }
  42.    
  43.     public ProblemRFC7807 fromString(String problemString) throws UtilsException {
  44.         return fromString(problemString, false);
  45.     }
  46.     public ProblemRFC7807 fromString(String problemString, boolean throwExceptionIfUnsupportedCustomClaim) throws UtilsException {
  47.         ObjectNode problemNode = (ObjectNode) this.jsonUtils.getAsNode(problemString);
  48.         ProblemRFC7807 p = new ProblemRFC7807();
  49.         p.setRaw(problemString);
  50.         return this._fromNode(p, problemNode, throwExceptionIfUnsupportedCustomClaim);
  51.     }
  52.     public ProblemRFC7807 fromByteArray(byte[] problemByteArray) throws UtilsException {
  53.         return fromByteArray(problemByteArray, false);
  54.     }
  55.     public ProblemRFC7807 fromByteArray(byte[] problemByteArray, boolean throwExceptionIfUnsupportedCustomClaim) throws UtilsException {
  56.         ObjectNode problemNode = (ObjectNode) this.jsonUtils.getAsNode(problemByteArray);
  57.         ProblemRFC7807 p = new ProblemRFC7807();
  58.         p.setRaw(new String(problemByteArray));
  59.         return this._fromNode(p, problemNode, throwExceptionIfUnsupportedCustomClaim);
  60.     }
  61.     public ProblemRFC7807 fromNode(ObjectNode problemNode) throws UtilsException {
  62.         return this._fromNode(null, problemNode, false);
  63.     }
  64.     public ProblemRFC7807 fromNode(ObjectNode problemNode, boolean throwExceptionIfUnsupportedCustomClaim) throws UtilsException {
  65.         return this._fromNode(null, problemNode, throwExceptionIfUnsupportedCustomClaim);
  66.     }
  67.     private ProblemRFC7807 _fromNode(ProblemRFC7807 problemParam, ObjectNode problemNode,  boolean throwExceptionIfUnsupportedCustomClaim) throws UtilsException {
  68.        
  69.         ProblemRFC7807 problem = null;
  70.         if(problemParam!=null) {
  71.             problem = problemParam;
  72.         }
  73.         else {
  74.             problem = new ProblemRFC7807();
  75.         }
  76.        
  77.         Iterator<String> it = problemNode.fieldNames();
  78.         while (it.hasNext()) {
  79.             String name = it.next();
  80.             Object value = problemNode.get(name);
  81.            
  82.             super.set(problem, name, value, throwExceptionIfUnsupportedCustomClaim);
  83.         }

  84.         if(problem.getRaw()==null) {
  85.             problem.setRaw(this.jsonUtils.toString(problemNode));
  86.         }
  87.        
  88.         return problem;
  89.     }
  90.    
  91. }