本文整理了Java中org.json.simple.JSONObject.size()
方法的一些代码示例,展示了JSONObject.size()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JSONObject.size()
方法的具体详情如下:
包路径:org.json.simple.JSONObject
类名称:JSONObject
方法名:size
暂无
代码示例来源:origin: Netflix/Priam
private void setExtraEnvParams(String extraEnvParams) {
try {
if (null != extraEnvParams && extraEnvParams.length() > 0) {
JSONParser parser = new JSONParser();
Object obj = parser.parse(extraEnvParams);
JSONObject jsonObj = (JSONObject) obj;
if (jsonObj.size() > 0) {
for (Iterator iterator = jsonObj.keySet().iterator(); iterator.hasNext(); ) {
String key = (String) iterator.next();
String val = (String) jsonObj.get(key);
if (StringUtils.isNotBlank(key) && StringUtils.isNotBlank(val)) {
System.setProperty(key.trim(), val.trim());
}
}
}
}
} catch (Exception e) {
System.out.println(
"Failed to parse extra env params: "
+ extraEnvParams
+ ". However, ignoring the exception.");
e.printStackTrace();
}
}
}
代码示例来源:origin: GlowstoneMC/Glowstone
/**
* Reloads from the file.
*/
public void load() {
entries.clear();
if (file.exists()) {
try (Reader reader = new FileReader(file)) {
JSONArray jsonArray = (JSONArray) new JSONParser().parse(reader);
for (Object object : jsonArray) {
JSONObject jsonObj = (JSONObject) object;
Map<String, String> map = new HashMap<>(jsonObj.size());
for (Object jsonEntry : jsonObj.entrySet()) {
Entry<?, ?> entry = (Map.Entry<?, ?>) jsonEntry;
map.put(entry.getKey().toString(), entry.getValue().toString());
}
entries.add(readEntry(map));
}
} catch (Exception ex) {
GlowServer.logger.log(Level.SEVERE, "Error reading from: " + file, ex);
}
} else {
//importLegacy();
save();
}
}
代码示例来源:origin: GlowstoneMC/Glowstone
} else {
JSONObject object = items.get(0);
if (object.size() == 1) {
代码示例来源:origin: rhuss/jolokia
private JSONObject updateHistoryForPatternReadWithMBeanAsPath(JmxReadRequest pJmxReq, long pTimestamp, Map<String, Object> pValues) {
// It the content of the MBean itself. MBean name is the first the single path part
String beanName = pJmxReq.getPathParts().get(0);
JSONObject ret = new JSONObject();
JSONObject beanHistory = addMultipleAttributeValues(
pJmxReq,
pValues,
beanName,
pTimestamp);
if (beanHistory.size() > 0) {
ret.put(beanName, beanHistory);
}
return ret;
}
代码示例来源:origin: apache/metron
@Test
public void testCollectionHandlingDrop() {
JSONMapParser parser = new JSONMapParser();
List<JSONObject> output = parser.parse(collectionHandlingJSON.getBytes());
Assert.assertEquals(output.size(), 1);
//don't forget the timestamp field!
Assert.assertEquals(output.get(0).size(), 2);
JSONObject message = output.get(0);
Assert.assertNotNull(message.get("timestamp"));
Assert.assertTrue(message.get("timestamp") instanceof Number);
}
代码示例来源:origin: apache/metron
@Test
public void testCollectionHandlingAllow() {
JSONMapParser parser = new JSONMapParser();
parser.configure(ImmutableMap.of(JSONMapParser.MAP_STRATEGY_CONFIG, JSONMapParser.MapStrategy.ALLOW.name()));
List<JSONObject> output = parser.parse(collectionHandlingJSON.getBytes());
Assert.assertEquals(output.size(), 1);
//don't forget the timestamp field!
Assert.assertEquals(output.get(0).size(), 3);
JSONObject message = output.get(0);
Assert.assertNotNull(message.get("timestamp"));
Assert.assertTrue(message.get("timestamp") instanceof Number);
}
代码示例来源:origin: apache/metron
@Test
public void testCollectionHandlingAllow() {
JSONMapParser parser = new JSONMapParser();
parser.configure(ImmutableMap
.of(JSONMapParser.MAP_STRATEGY_CONFIG, JSONMapParser.MapStrategy.ALLOW.name(),
JSONMapParser.JSONP_QUERY, "$.foo"));
List<JSONObject> output = parser.parse(collectionHandlingJSON.getBytes());
Assert.assertEquals(output.size(), 2);
Assert.assertEquals(output.get(0).size(), 3);
JSONObject message = output.get(0);
Assert.assertNotNull(message.get("timestamp"));
Assert.assertTrue(message.get("timestamp") instanceof Number);
Assert.assertEquals(output.get(1).size(), 3);
message = output.get(1);
Assert.assertNotNull(message.get("timestamp"));
Assert.assertTrue(message.get("timestamp") instanceof Number);
}
代码示例来源:origin: apache/metron
@Test
public void testCollectionHandlingAllow() {
JSONMapParser parser = new JSONMapParser();
parser.configure(ImmutableMap
.of(JSONMapParser.MAP_STRATEGY_CONFIG, JSONMapParser.MapStrategy.ALLOW.name(),
JSONMapParser.JSONP_QUERY, "$.foo"));
List<JSONObject> output = parser.parse(collectionHandlingJSON.getBytes());
Assert.assertEquals(output.size(), 2);
Assert.assertEquals(output.get(0).size(), 3);
JSONObject message = output.get(0);
Assert.assertNotNull(message.get("timestamp"));
Assert.assertTrue(message.get("timestamp") instanceof Number);
Assert.assertEquals(output.get(1).size(), 3);
message = output.get(1);
Assert.assertNotNull(message.get("timestamp"));
Assert.assertTrue(message.get("timestamp") instanceof Number);
}
代码示例来源:origin: apache/metron
@Test
public void testMixedCollectionHandlingUnfold() {
JSONMapParser parser = new JSONMapParser();
parser.configure(ImmutableMap.of(JSONMapParser.MAP_STRATEGY_CONFIG,JSONMapParser.MapStrategy.UNFOLD.name()));
List<JSONObject> output = parser.parse(mixCollectionHandlingJSON.getBytes());
Assert.assertEquals(output.get(0).size(), 4);
JSONObject message = output.get(0);
Assert.assertEquals(message.get("collection.key"), "value");
Assert.assertEquals(message.get("key"),"value");
Assert.assertNotNull(message.get("timestamp"));
Assert.assertTrue(message.get("timestamp") instanceof Number );
}
}
代码示例来源:origin: apache/metron
@Test
public void testHappyPath() {
JSONMapParser parser = new JSONMapParser();
List<JSONObject> output = parser.parse(happyPathJSON.getBytes());
Assert.assertEquals(output.size(), 1);
//don't forget the timestamp field!
Assert.assertEquals(output.get(0).size(), 5);
JSONObject message = output.get(0);
Assert.assertEquals("bar", message.get("foo"));
Assert.assertEquals("blah", message.get("blah"));
Assert.assertNotNull(message.get("timestamp"));
Assert.assertTrue(message.get("timestamp") instanceof Number);
Assert.assertNotNull(message.get("number"));
Assert.assertTrue(message.get("number") instanceof Number);
}
代码示例来源:origin: apache/metron
private void testMapEnrichment(String config, String field) throws Exception {
JSONObject message = getMessage();
EnrichmentConfig enrichmentConfig = JSONUtils.INSTANCE.load(config, EnrichmentConfig.class);
Assert.assertNotNull(enrichmentConfig.getEnrichmentConfigs().get("stellar"));
ConfigHandler handler = enrichmentConfig.getEnrichmentConfigs().get("stellar");
JSONObject enriched = enrich(message, field, handler);
Assert.assertEquals(2, enriched.size());
Assert.assertEquals("stellar_test", enriched.get("stmt2.foo"));
Assert.assertEquals("stellar_test".toUpperCase(), enriched.get("stmt1"));
}
代码示例来源:origin: apache/metron
@Test
public void testCollectionHandlingUnfold() {
JSONMapParser parser = new JSONMapParser();
parser.configure(ImmutableMap.of(JSONMapParser.MAP_STRATEGY_CONFIG, JSONMapParser.MapStrategy.UNFOLD.name()));
List<JSONObject> output = parser.parse(collectionHandlingJSON.getBytes());
Assert.assertEquals(output.size(), 1);
//don't forget the timestamp field!
Assert.assertEquals(output.get(0).size(), 6);
JSONObject message = output.get(0);
Assert.assertEquals(message.get("collection.blah"), 7);
Assert.assertEquals(message.get("collection.blah2"), "foo");
Assert.assertEquals(message.get("collection.bigblah.innerBlah"),"baz");
Assert.assertEquals(message.get("collection.bigblah.reallyInnerBlah.color"),"grey");
Assert.assertNotNull(message.get("timestamp"));
Assert.assertTrue(message.get("timestamp") instanceof Number);
}
代码示例来源:origin: apache/metron
@Test
public void testConfigAll() throws Exception {
SensorParserConfig c = SensorParserConfig.fromBytes(Bytes.toBytes(configAll));
JSONObject input = new JSONObject();
input.put("source.type", "test");
for (FieldTransformer handler : c.getFieldTransformations()) {
handler.transformAndUpdate(input, Context.EMPTY_CONTEXT());
}
Assert.assertEquals(2, input.size());
Assert.assertTrue(input.containsKey("new_field"));
Assert.assertEquals("test", input.get("new_field"));
}
代码示例来源:origin: apache/metron
@Test
public void testCollectionHandlingDrop() {
JSONMapParser parser = new JSONMapParser();
parser.configure(new HashMap<String, Object>() {{
put(JSONMapParser.JSONP_QUERY, "$.foo");
}});
List<JSONObject> output = parser.parse(collectionHandlingJSON.getBytes());
Assert.assertEquals(output.size(), 2);
//don't forget the timestamp field!
Assert.assertEquals(output.get(0).size(), 2);
JSONObject message = output.get(0);
Assert.assertNotNull(message.get("timestamp"));
Assert.assertTrue(message.get("timestamp") instanceof Number);
message = output.get(1);
Assert.assertNotNull(message.get("timestamp"));
Assert.assertTrue(message.get("timestamp") instanceof Number);
}
代码示例来源:origin: apache/metron
@Test
public void testCollectionHandlingDrop() {
JSONMapParser parser = new JSONMapParser();
parser.configure(new HashMap<String, Object>() {{
put(JSONMapParser.JSONP_QUERY, "$.foo");
}});
List<JSONObject> output = parser.parse(collectionHandlingJSON.getBytes());
Assert.assertEquals(output.size(), 2);
//don't forget the timestamp field!
Assert.assertEquals(output.get(0).size(), 2);
JSONObject message = output.get(0);
Assert.assertNotNull(message.get("timestamp"));
Assert.assertTrue(message.get("timestamp") instanceof Number);
message = output.get(1);
Assert.assertNotNull(message.get("timestamp"));
Assert.assertTrue(message.get("timestamp") instanceof Number);
}
代码示例来源:origin: apache/metron
@Test
public void test_default() throws Exception {
for(String c : DEFAULT_CONFIGS) {
JSONObject message = getMessage();
EnrichmentConfig enrichmentConfig = JSONUtils.INSTANCE.load(c, EnrichmentConfig.class);
Assert.assertNotNull(enrichmentConfig.getEnrichmentConfigs().get("stellar"));
ConfigHandler handler = enrichmentConfig.getEnrichmentConfigs().get("stellar");
JSONObject enriched = enrich(message, "", handler);
Assert.assertEquals("STELLAR_TEST", enriched.get("stmt1"));
Assert.assertEquals("stellar_test", enriched.get("stmt2"));
Assert.assertEquals("foo", enriched.get("stmt3"));
Assert.assertEquals(3, enriched.size());
}
}
代码示例来源:origin: apache/metron
@Test
public void renameMissingField() throws Exception {
SensorParserConfig c = SensorParserConfig.fromBytes(Bytes.toBytes(renameMissingField));
FieldTransformer handler = Iterables.getFirst(c.getFieldTransformations(), null);
JSONObject input = new JSONObject(new HashMap<String, Object>() {{
for(int i = 2;i <= 10;++i) {
put("old_field" + i, "f" + i);
}
}});
handler.transformAndUpdate(input, Context.EMPTY_CONTEXT());
Assert.assertFalse(input.containsKey("new_field1"));
for(int i = 2;i <= 10;++i) {
Assert.assertEquals("f" + i, input.get("old_field" + i));
}
Assert.assertEquals(9, input.size());
}
}
代码示例来源:origin: apache/metron
@Test
public void testSingleFieldReturned() throws Exception {
SensorParserConfig sensorConfig = SensorParserConfig.fromBytes(Bytes.toBytes(selectSingleFieldConfig));
FieldTransformer handler = Iterables.getFirst(sensorConfig.getFieldTransformations(), null);
JSONObject input = new JSONObject(new HashMap<String, Object>() {
{
put("field1", "foo");
put("field2", "bar");
}
});
handler.transformAndUpdate(input, Context.EMPTY_CONTEXT());
Assert.assertTrue(input.containsKey("field1"));
Assert.assertFalse(input.containsKey("field2"));
Assert.assertEquals(1, input.size());
}
代码示例来源:origin: apache/metron
@Test
public void testMulitpleFieldReturned() throws Exception {
SensorParserConfig sensorConfig = SensorParserConfig.fromBytes(Bytes.toBytes(selectMultiFieldConfig));
FieldTransformer handler = Iterables.getFirst(sensorConfig.getFieldTransformations(), null);
JSONObject input = new JSONObject(new HashMap<String, Object>() {
{
put("field1", "foo");
put("field2", "bar");
put("field3", "bar2");
}
});
handler.transformAndUpdate(input, Context.EMPTY_CONTEXT());
Assert.assertTrue(input.containsKey("field1"));
Assert.assertTrue(input.containsKey("field2"));
Assert.assertFalse(input.containsKey("field3"));
Assert.assertEquals(2, input.size());
}
代码示例来源:origin: apache/metron
@Test
public void testNullEnrichment() throws Exception {
SensorEnrichmentConfig config = JSONUtils.INSTANCE.load(nullConfig, SensorEnrichmentConfig.class);
config.getConfiguration().putIfAbsent("stellarContext", stellarContext);
JSONObject message = new JSONObject() {{
put(Constants.SENSOR_TYPE, "test");
}};
ParallelEnricher.EnrichmentResult result = enricher.apply(message, EnrichmentStrategies.ENRICHMENT, config, null);
JSONObject ret = result.getResult();
Assert.assertEquals("Got the wrong result count: " + ret, 7, ret.size());
Assert.assertTrue(result.getResult().containsKey("adapter.dummyenrichmentadapter.begin.ts"));
Assert.assertTrue(result.getResult().containsKey("adapter.dummyenrichmentadapter.end.ts"));
Assert.assertTrue(result.getResult().containsKey("parallelenricher.splitter.begin.ts"));
Assert.assertTrue(result.getResult().containsKey("parallelenricher.splitter.end.ts"));
Assert.assertTrue(result.getResult().containsKey("parallelenricher.enrich.begin.ts"));
Assert.assertTrue(result.getResult().containsKey("parallelenricher.enrich.end.ts"));
}
内容来源于网络,如有侵权,请联系作者删除!