Git Product home page Git Product logo

Comments (1)

bpsm avatar bpsm commented on June 24, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.