Java Code Examples for net.minidev.json.parser.JSONParser#parse()

The following examples show how to use net.minidev.json.parser.JSONParser#parse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PyLint.java    From warnings-ng-plugin with MIT License 6 votes vote down vote up
int initialize() {
    JSONParser parser = new JSONParser(JSONParser.MODE_JSON_SIMPLE);

    try {
        JSONArray elements = (JSONArray) parser.parse(PyLint.class.getResourceAsStream("pylint-descriptions.json"));
        for (Object element : elements) {
            JSONObject object = (JSONObject) element;
            String description = object.getAsString("description");
            descriptionByName.put(object.getAsString("name"), description);
            descriptionById.put(object.getAsString("code"), description);
        }
    }
    catch (ParseException | UnsupportedEncodingException ignored) {
        // ignore all exceptions
    }

    return descriptionByName.size();
}
 
Example 2
Source File: TokenUtil.java    From microservice-with-jwt-and-microprofile with Apache License 2.0 6 votes vote down vote up
/**
 * Utility method to generate a JWT string from a JSON resource file that is signed by the privateKey-pkcs1.pem
 * test resource key, possibly with invalid fields.
 *
 * @param jsonResName   - name of test resources file
 * @param invalidClaims - the set of claims that should be added with invalid values to test failure modes
 * @param timeClaims    - used to return the exp, iat, auth_time claims
 * @return the JWT string
 * @throws Exception on parse failure
 */
public static String generateTokenString(String jsonResName, Set<InvalidClaims> invalidClaims, Map<String, Long> timeClaims) throws Exception {
    if (invalidClaims == null) {
        invalidClaims = Collections.emptySet();
    }
    final InputStream contentIS = TokenUtil.class.getResourceAsStream(jsonResName);
    byte[] tmp = new byte[4096];
    int length = contentIS.read(tmp);
    byte[] content = new byte[length];
    System.arraycopy(tmp, 0, content, 0, length);

    JSONParser parser = new JSONParser(JSONParser.DEFAULT_PERMISSIVE_MODE);
    JSONObject jwtContent = (JSONObject) parser.parse(content);

    return generateTokenString(jwtContent, invalidClaims, timeClaims);
}
 
Example 3
Source File: JSONValue.java    From json-smart-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Parse input json as a mapTo class
 * 
 * @since 2.0
 */
protected static <T> T parse(byte[] in, JsonReaderI<T> mapper) {
	try {
		JSONParser p = new JSONParser(DEFAULT_PERMISSIVE_MODE);
		return p.parse(in, mapper);
	} catch (Exception e) {
		return null;
	}
}
 
Example 4
Source File: TokenUtils.java    From tomee with Apache License 2.0 5 votes vote down vote up
public static String generateJWTString(String jsonResource, String keyId) throws Exception {
    byte[] byteBuffer = new byte[16384];
    currentThread().getContextClassLoader()
            .getResource(jsonResource)
            .openStream()
            .read(byteBuffer);

    JSONParser parser = new JSONParser(DEFAULT_PERMISSIVE_MODE);
    JSONObject jwtJson = (JSONObject) parser.parse(byteBuffer);

    long currentTimeInSecs = (System.currentTimeMillis() / 1000);
    long expirationTime = currentTimeInSecs + 1000;

    jwtJson.put(Claims.iat.name(), currentTimeInSecs);
    jwtJson.put(Claims.auth_time.name(), currentTimeInSecs);
    jwtJson.put(Claims.exp.name(), expirationTime);

    SignedJWT signedJWT = new SignedJWT(new JWSHeader
            .Builder(RS256)
            .keyID(keyId) // /privateKey002.pem
            .type(JWT)
            .build(), parse(jwtJson));

    signedJWT.sign(new RSASSASigner(readPrivateKey(keyId)));

    return signedJWT.serialize();
}
 
Example 5
Source File: TokenUtils.java    From tomee with Apache License 2.0 5 votes vote down vote up
public static String generateJWTString(String jsonResource) throws Exception {
    byte[] byteBuffer = new byte[16384];
    currentThread().getContextClassLoader()
            .getResource(jsonResource)
            .openStream()
            .read(byteBuffer);

    JSONParser parser = new JSONParser(DEFAULT_PERMISSIVE_MODE);
    JSONObject jwtJson = (JSONObject) parser.parse(byteBuffer);

    long currentTimeInSecs = (System.currentTimeMillis() / 1000);
    long expirationTime = currentTimeInSecs + 1000;

    jwtJson.put(Claims.iat.name(), currentTimeInSecs);
    jwtJson.put(Claims.auth_time.name(), currentTimeInSecs);
    jwtJson.put(Claims.exp.name(), expirationTime);

    SignedJWT signedJWT = new SignedJWT(new JWSHeader
            .Builder(RS256)
            .keyID("/privateKey.pem")
            .type(JWT)
            .build(), parse(jwtJson));

    signedJWT.sign(new RSASSASigner(readPrivateKey("privateKey.pem")));

    return signedJWT.serialize();
}
 
Example 6
Source File: JSONValue.java    From json-smart-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Parse input json as a mapTo class
 * 
 * mapTo can be a bean
 * 
 * @since 2.0
 */
public static <T> T parse(String in, Class<T> mapTo) {
	try {
		JSONParser p = new JSONParser(DEFAULT_PERMISSIVE_MODE);
		return p.parse(in, defaultReader.getMapper(mapTo));
	} catch (Exception e) {
		return null;
	}
}
 
Example 7
Source File: MustThrows.java    From json-smart-v2 with Apache License 2.0 5 votes vote down vote up
public static void testInvalidJson(String json, int permissifMode, int execptionType, Class<?> cls)
		throws Exception {
	JSONParser p = new JSONParser(permissifMode);
	try {
		if (cls == null)
			p.parse(json);
		else
			p.parse(json, cls);
		TestCase.assertFalse("Exception Should Occure parsing:" + json, true);
	} catch (ParseException e) {
		if (execptionType == -1)
			execptionType = e.getErrorType();
		TestCase.assertEquals(execptionType, e.getErrorType());
	}
}
 
Example 8
Source File: JSONValue.java    From json-smart-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Parse input json as a mapTo class
 * 
 * mapTo can be a bean
 * 
 * @since 2.0
 */
public static <T> T parse(Reader in, T toUpdate) {
	try {
		JSONParser p = new JSONParser(DEFAULT_PERMISSIVE_MODE);
		return p.parse(in, new UpdaterMapper<T>(defaultReader, toUpdate));
	} catch (Exception e) {
		return null;
	}
}
 
Example 9
Source File: JSONValue.java    From json-smart-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Parse input json as a mapTo class
 * 
 * mapTo can be a bean
 * 
 * @since 2.0
 */
public static <T> T parse(Reader in, Class<T> mapTo) {
	try {
		JSONParser p = new JSONParser(DEFAULT_PERMISSIVE_MODE);
		return p.parse(in, defaultReader.getMapper(mapTo));
	} catch (Exception e) {
		return null;
	}
}
 
Example 10
Source File: JSONValue.java    From json-smart-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Parse input json as a mapTo class
 * 
 * mapTo can be a bean
 * 
 * @since 2.0
 */
public static <T> T parse(byte[] in, Class<T> mapTo) {
	try {
		JSONParser p = new JSONParser(DEFAULT_PERMISSIVE_MODE);
		return p.parse(in, defaultReader.getMapper(mapTo));
	} catch (Exception e) {
		return null;
	}
}
 
Example 11
Source File: EleConn.java    From jelectrum with MIT License 5 votes vote down vote up
public void run()
{
  try
  {
    JSONParser parser = new JSONParser(JSONParser.MODE_STRICTEST);

    while(sock.isConnected())
    {
      String line = in_scan.nextLine();
      if (VERBOSE)
      {
        System.out.println("In: " + line);
      }
      JSONObject o = (JSONObject) parser.parse(line);

      int id = (int)o.get("id");

      getQueue(id).put(o);
    }
  }
  catch(Exception e)
  {
    System.out.println(e);
    close();
  }

}
 
Example 12
Source File: KafkaZookeeperURLManager.java    From knox with Apache License 2.0 5 votes vote down vote up
/**
 * Given a String of the format "{"jmx_port":-1,"timestamp":"1505763958072","endpoints":["PLAINTEXT://host:6667"],"host":"host","version":3,"port":6667}"
 * convert to a URL of the format "http://host:port".
 *
 * @param serverInfo Server Info in JSON Format from Zookeeper (required)
 *
 * @return URL to Kafka
 * @throws ParseException failure parsing json from string
 */
private String constructURL(String serverInfo) throws ParseException {
  String scheme = "http";

  StringBuilder buffer = new StringBuilder();

  buffer.append(scheme).append("://");

  JSONParser parser = new JSONParser(JSONParser.DEFAULT_PERMISSIVE_MODE);
  JSONObject obj = (JSONObject) parser.parse(serverInfo);
  buffer.append(obj.get("host")).append(':').append(PORT_NUMBER);

  return buffer.toString();
}
 
Example 13
Source File: JSONValue.java    From json-smart-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Parse input json as a mapTo class
 * 
 * @since 2.0
 */
protected static <T> T parse(String in, JsonReaderI<T> mapper) {
	try {
		JSONParser p = new JSONParser(DEFAULT_PERMISSIVE_MODE);
		return p.parse(in, mapper);
	} catch (Exception e) {
		return null;
	}
}
 
Example 14
Source File: TestFloat.java    From json-smart-v2 with Apache License 2.0 5 votes vote down vote up
public void testNonFloat() throws Exception {
	JSONParser p = new JSONParser(JSONParser.MODE_PERMISSIVE);
	for (String s : FALSE_NUMBERS) {
		String json = "{v:" + s + "}";
		JSONObject obj = (JSONObject) p.parse(json);
		assertEquals("Should be parse as string", s, obj.get("v"));

		String correct = "{\"v\":\"" + s + "\"}";
		assertEquals("Should be re serialized as", correct, obj.toJSONString());
	}
}
 
Example 15
Source File: JwtTokenGenerator.java    From piranha with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static String generateJWTString(String jsonResource) throws Exception {
    byte[] byteBuffer = new byte[16384];
    currentThread().getContextClassLoader()
                   .getResource(jsonResource)
                   .openStream()
                   .read(byteBuffer);

    JSONParser parser = new JSONParser(DEFAULT_PERMISSIVE_MODE);
    JSONObject jwtJson = (JSONObject) parser.parse(byteBuffer);
    
    long currentTimeInSecs = (System.currentTimeMillis() / 1000);
    long expirationTime = currentTimeInSecs + 1000;
   
    jwtJson.put(Claims.iat.name(), currentTimeInSecs);
    jwtJson.put(Claims.auth_time.name(), currentTimeInSecs);
    jwtJson.put(Claims.exp.name(), expirationTime);
    
    SignedJWT signedJWT = new SignedJWT(new JWSHeader
                                        .Builder(RS256)
                                        .keyID("/privateKey.pem")
                                        .type(JWT)
                                        .build(), parse(jwtJson));
    
    signedJWT.sign(new RSASSASigner(readPrivateKey("privateKey.pem")));
    
    return signedJWT.serialize();
}
 
Example 16
Source File: JwtTokenGenerator.java    From piranha with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static String generateJWTString(String jsonResource) throws Exception {
    byte[] byteBuffer = new byte[16384];
    currentThread().getContextClassLoader()
                   .getResource(jsonResource)
                   .openStream()
                   .read(byteBuffer);

    JSONParser parser = new JSONParser(DEFAULT_PERMISSIVE_MODE);
    JSONObject jwtJson = (JSONObject) parser.parse(byteBuffer);
    
    long currentTimeInSecs = (System.currentTimeMillis() / 1000);
    long expirationTime = currentTimeInSecs + 1000;
   
    jwtJson.put(Claims.iat.name(), currentTimeInSecs);
    jwtJson.put(Claims.auth_time.name(), currentTimeInSecs);
    jwtJson.put(Claims.exp.name(), expirationTime);
    
    SignedJWT signedJWT = new SignedJWT(new JWSHeader
                                        .Builder(RS256)
                                        .keyID("/privateKey.pem")
                                        .type(JWT)
                                        .build(), parse(jwtJson));
    
    signedJWT.sign(new RSASSASigner(readPrivateKey("privateKey.pem")));
    
    return signedJWT.serialize();
}
 
Example 17
Source File: RpcUtil.java    From snowblossom with Apache License 2.0 5 votes vote down vote up
public static JSONObject protoToJson(com.google.protobuf.Message m)
  throws Exception
{
  JsonFormat.Printer printer = JsonFormat.printer();
  String str = printer.print(m);

  JSONParser parser = new JSONParser(JSONParser.MODE_STRICTEST);
  return (JSONObject) parser.parse(str);

}
 
Example 18
Source File: JSONSimpleTest.java    From json-smart-v2 with Apache License 2.0 4 votes vote down vote up
public void testDefault() throws Exception {
	String s = "[1]";
	JSONParser p = new JSONParser(JSONParser.MODE_PERMISSIVE);
	JSONArray array = (JSONArray) p.parse(s);
	assertEquals(Integer.valueOf(1), (Integer) array.get(0));
}
 
Example 19
Source File: JSONSimpleTest.java    From json-smart-v2 with Apache License 2.0 4 votes vote down vote up
public void testLong() throws Exception {
	String s = "[1]";
	JSONParser p = new JSONParser(JSONParser.MODE_JSON_SIMPLE);
	JSONArray array = (JSONArray) p.parse(s);
	assertEquals(Long.valueOf(1), (Long) array.get(0));
}
 
Example 20
Source File: JSONValue.java    From json-smart-v2 with Apache License 2.0 2 votes vote down vote up
/**
 * Parse input json as a mapTo class
 * 
 * mapTo can be a bean
 * 
 * @since 2.0
 */
public static <T> T parseWithException(String in, Class<T> mapTo) throws ParseException {
	JSONParser p = new JSONParser(DEFAULT_PERMISSIVE_MODE);
	return p.parse(in, defaultReader.getMapper(mapTo));
}