Comments (1)
So you have a map that looks like this:
{
:modules [
{:active 1, :addr 10657, :sensors [520, 519, 0, 0]},
{:active 0, :addr 8217, :sensors [212, 520, 0, 0]},
{:active 0, :addr 0, :sensors [0, 0, 0, 0]}
]
}
I guess the example you quoted above is just what Java's toString() spits out as it's not syntactically correct edn. (You'll have to use a Printer for that.)
You can iterate through the modulesList using a for(:) loop, but be aware that you'll have to do a fair amount of type casting. This is probably one of those places where you can just use raw types instead of trying to formulate everything with generics, since that just leaves you with wildcards all over the place. (Edn is dynamically typed, Java is not ā¦ I'm afraid this ugliness is just part of bridging that gap.)
package us.bpsm.edn.issues;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import us.bpsm.edn.Keyword;
import us.bpsm.edn.parser.Parseable;
import us.bpsm.edn.parser.Parser;
import us.bpsm.edn.parser.Parsers;
import static org.junit.Assert.assertEquals;
import static us.bpsm.edn.Keyword.newKeyword;
import static us.bpsm.edn.parser.Parsers.defaultConfiguration;
public class Issue39Test {
static final Keyword MODULES = newKeyword("modules");
static final Keyword ACTIVE = newKeyword("active");
static final Keyword ADDR = newKeyword("addr");
static final Keyword SENSORS = newKeyword("sensors");
static final String MODULES_EDN =
"{\n" +
" :modules [\n"+
" {:active 1, :addr 10657, :sensors [520, 519, 0, 0]},\n"+
" {:active 0, :addr 8217, :sensors [212, 520, 0, 0]},\n" +
" {:active 0, :addr 0, :sensors [0, 0, 0, 0]}\n" +
" ]\n" +
"}\n";
@Test
public void test() {
Parseable pbr = Parsers.newParseable(MODULES_EDN);
Parser p = Parsers.newParser(defaultConfiguration());
Map<?, ?> m = (Map<?, ?>) p.nextValue(pbr);
List<?> modulesList = (List<?>) m.get(MODULES);
// This just shows that the modulesList contains what we expect
expectModule((Map<?,?>)modulesList.get(0), 1, 10657, 520, 519, 0, 0);
expectModule((Map<?,?>)modulesList.get(1), 0, 8217, 212, 520, 0, 0);
expectModule((Map<?,?>)modulesList.get(2), 0, 0, 0, 0, 0, 0);
// This is an example of how one might iterate over the contents of the
// module List. Yes all the casting is ugly.
for (Object o: modulesList) {
Map<?,?> module = (Map<?,?>) o;
doSomethingWithModuleMap(module);
}
}
private void expectModule(Map<?,?> module, int active, int addr, int ... sensors) {
assertEquals(active, ((Number)module.get(ACTIVE)).intValue());
assertEquals(addr, ((Number)module.get(ADDR)).intValue());
List<Number> moduleSensorsList = (List<Number>) module.get(SENSORS);
assertEquals(sensors.length, moduleSensorsList.size());
for (int i = 0; i < sensors.length; i++) {
assertEquals(sensors[i], moduleSensorsList.get(i).intValue());
}
}
private void doSomethingWithModuleMap(Map<?,?> module) {
System.out.println(module);
}
}
Working with raw maps and lists is not generally idiomatic in Java, so it's not very convenient to do. Usually Java programs suck this kind of dynamically typed data into some kind of a statically typed object model for manipulation. (Iām not claiming that this is a good idea, but it is idiomatic.)
In the case of edn-java one way to do this mapping is by traversing the data structure returned by Parser.nextValue()
recursively and building your model objects by hand. This basically implies that the results of nextValue()
will have a known shape. Another approach is to use tagged literals, allowing you to install your translation methods in the parser configuration as tag handlers, but this implies that you have control over the input format.
from edn-java.
Related Issues (20)
- > and < symbols are not supported HOT 3
- Values returned by Parser should be Serializable by default HOT 1
- Comma character prints incorrectly HOT 3
- PrintingExamples are sensitive to hash ordering HOT 1
- Teach edn-java to read namespaced maps as per CLJ-1910 HOT 1
- Teach edn-java to optionally print namespaced maps as per CLJ-1910
- Duplicate map keys are parsed without error HOT 5
- Add Java 8 to test with Travis too. HOT 1
- QuickCheck to test? HOT 1
- mispelling, in the docs
- Version 1.0? HOT 1
- Reader flag to support unicode escapes in string literals HOT 14
- Support reading \uXXXX character literals HOT 1
- Review edn-format; compare to implementation HOT 1
- Support # embedded in symbol names HOT 1
- Clojure's edn-read allows unicode in names, should edn-java do the same?
- Fix broken GPG magic HOT 4
- Octal escapes in string and character literals? HOT 1
- 0.7.0 or 0.7.1 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
š Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ššš
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ā¤ļø Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from edn-java.