-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Iterate over list recieved via get. #39
Comments
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 |
Am storing parsed EDN in a map
m
Under the keyword
modules
I have a list of maps:That's parsed. (if I
println(m.get(Keyword.newKeyword("modules")));
)My question is how would I iterate through the [] list and access each of the maps directly. ( I haven't been able to do it as I am getting an error "... cannot convert from capture#2-of ? to ..." )
Thanks.
The text was updated successfully, but these errors were encountered: