コード例 #1
0
 @Override
 public Year deserialize(JsonParser parser, DeserializationContext context) throws IOException {
   if (formatter == null) {
     return Year.of(parser.getValueAsInt());
   }
   return Year.parse(parser.getValueAsString(), formatter);
 }
コード例 #2
0
 /** Sends a POST request to obtain an access token. */
 private void obtainAccessToken() {
   try {
     token = new OAuth2Token();
     error = new OAuth2Error();
     /* build the request and send it to the token server */
     CloseableHttpClient client = HttpClients.createDefault();
     HttpPost request = new HttpPost(tokenServer);
     request.setEntity(new UrlEncodedFormEntity(Utils.mapToList(tokenParams)));
     CloseableHttpResponse response = client.execute(request);
     HttpEntity entity = response.getEntity();
     /* get the response and parse it */
     JsonParser jp = json.getFactory().createParser(entity.getContent());
     while (jp.nextToken() != null) {
       JsonToken jsonToken = jp.getCurrentToken();
       switch (jsonToken) {
         case FIELD_NAME:
           String name = jp.getCurrentName();
           jsonToken = jp.nextToken();
           if (name.equals("access_token")) {
             token.setAccessToken(jp.getValueAsString());
           } else if (name.equals("token_type")) {
             token.setType(OAuth2TokenType.valueOf(jp.getValueAsString().toUpperCase()));
           } else if (name.equals("expires_in")) {
             token.setExpiresIn(jp.getValueAsInt());
           } else if (name.equals("refresh_token")) {
             token.setRefreshToken(jp.getValueAsString());
           } else if (name.equals("kid")) {
             token.setKeyId(jp.getValueAsString());
           } else if (name.equals("mac_key")) {
             token.setMacKey(jp.getValueAsString());
           } else if (name.equals("mac_algorithm")) {
             token.setMacAlgorithm(jp.getValueAsString());
           } else if (name.equals("error")) {
             error.setType(OAuth2ErrorType.valueOf(jp.getValueAsString().toUpperCase()));
           } else if (name.equals("error_description")) {
             error.setDescription(jp.getValueAsString());
           } else if (name.equals("error_uri")) {
             error.setUri(jp.getValueAsString());
           }
           ready = true;
           break;
         default:
           break;
       }
     }
     jp.close();
     response.close();
     client.close();
   } catch (IOException e) {
     error.setType(OAuth2ErrorType.SERVER_ERROR);
     error.setDescription("Failed to obtain access token from the server.");
   }
   /* notify all waiting objects */
   synchronized (waitObject) {
     ready = true;
     waitObject.notifyAll();
   }
 }
コード例 #3
0
 public static boolean processSingleField(Fgsyudlview instance, String fieldName, JsonParser jp)
     throws IOException {
   if ("BRANCH".equals(fieldName)) {
     instance.BRANCH = jp.getValueAsString();
     return true;
   } else if ("FGSDES".equals(fieldName)) {
     instance.FGSDES = jp.getValueAsString();
     return true;
   } else if ("MONTH".equals(fieldName)) {
     instance.MONTH = jp.getValueAsString();
   } else if ("SWDL".equals(fieldName)) {
     instance.SWDL = jp.getValueAsInt();
   } else if ("XDL".equals(fieldName)) {
     instance.XDL = jp.getValueAsInt();
   } else if ("YEAR".equals(fieldName)) {
     instance.YEAR = jp.getValueAsString();
   }
   return false;
 }
コード例 #4
0
 @Override
 public Object deserialize(JsonParser jp, DeserializationContext ctxt)
     throws IOException, JsonProcessingException {
   // couple of accepted types...
   Object value;
   if (_inputType == null) {
     value = jp.getText();
   } else if (_inputType == Integer.class) {
     value = Integer.valueOf(jp.getValueAsInt());
   } else if (_inputType == Long.class) {
     value = Long.valueOf(jp.getValueAsLong());
   } else {
     throw ctxt.mappingException(_enumClass);
   }
   try {
     return _factory.invoke(_enumClass, value);
   } catch (Exception e) {
     ClassUtil.unwrapAndThrowAsIAE(e);
   }
   return null;
 }
 @Override
 public Boolean deserialize(JsonParser jp, DeserializationContext ctxt)
     throws IOException, JsonProcessingException {
   return jp.getValueAsInt(0) <= 0 ? Boolean.FALSE : Boolean.TRUE;
 }
コード例 #6
0
ファイル: JSONParser.java プロジェクト: Delphian2015/FirstRep
  public void jsonParser() {
    int iterator = 0;
    Manager manager = Manager.getInstance(); // Create the object of singleton
    Element jacksonElement = new Element();

    JsonFactory jsonFactory = new JsonFactory();
    try {
      File f = new File(FileDownload.getFileName());
      JsonParser parser = jsonFactory.createParser(f);

      // Parse the file  by Jackson parser
      while (!parser.isClosed()) {
        JsonToken jsonToken = parser.nextToken();

        if (JsonToken.FIELD_NAME.equals(jsonToken)) {
          String fieldName = parser.getCurrentName();
          jsonToken = parser.nextToken();

          // The JSON file has the next fields:
          // location,date,name and object stock (this object
          // includes:name,bid,id,visible,minPrice,maxPrice)

          if ("location".equals(fieldName)) {
            manager.setLocation(parser.getValueAsString());
          }
          if ("date".equals(fieldName)) {
            manager.setDate(parser.getValueAsString());
          }
          if ("name".equals(fieldName) && (parser.getValueAsString().equals("Moscow Stock")))
            manager.setName(parser.getValueAsString());

          if ("bid".equals(fieldName)) {
            jacksonElement.setBid(parser.getValueAsDouble());
            iterator++;
          } else if ("id".equals(fieldName)) {
            jacksonElement.setId(parser.getValueAsInt());
            iterator++;
          } else if ("name".equals(fieldName)
              && (!parser.getValueAsString().equals("Moscow Stock"))) {
            jacksonElement.setName(
                parser.getValueAsString().substring(0, parser.getValueAsString().length() - 4));
            iterator++;
          } else if ("minPrice".equals(fieldName)) {
            jacksonElement.setMinPrice(parser.getValueAsDouble());
            iterator++;
          } else if ("maxPrice".equals(fieldName)) {
            jacksonElement.setMaxPrice(parser.getValueAsDouble());
            iterator++;
          } else if ("visible".equals(fieldName)) {
            jacksonElement.setVisible(parser.getValueAsBoolean());
            iterator++;
          }
          if (iterator == 6) {
            manager.setJsonList(jacksonElement); // add the data to the list
            jacksonElement = new Element();
            iterator = 0;
          }
        }
      }
    } catch (JsonParseException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
コード例 #7
0
 @Override
 public Integer deserialize(JsonParser jp, DeserializationContext ctxt)
     throws IOException, JsonProcessingException {
   return jp.getValueAsInt() - 10;
 }