Discussion:
[picocontainer-dev] JSON Pico container
Serban Iordache
2013-12-13 19:00:38 UTC
Permalink
Hi all,
I would like to contribute a pico container that populates itself by means of a configuration file in JSON format.
For a usage example, let's consider the following code fragment:
public class Banner extends JFrame { public Banner(JTextComponent textComp, String text, int size, boolean bold) { textComp.setText(text); textComp.setFont(new Font("Arial", (bold ? Font.BOLD : Font.PLAIN), size)); add(textComp); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); pack(); }
public static void main(String[] args) { MutablePicoContainer pico = new DefaultPicoContainer(); pico.addComponent("textComp", JTextArea.class, DefaultConstructorParameter.INSTANCE); pico.addComponent("banner", Banner.class, new ComponentParameter("textComp"), new ConstantParameter("Hello, world!"), new ComponentParameter("size"), new ComponentParameter("bold")); pico.addConfig("size", 36); pico.addConfig("bold", true);
Banner banner = (Banner)pico.getComponent("banner"); banner.setVisible(true); } }
Using the JsonPicoContainer, the main method above can be rewritten as:
public static void main(String[] args) { MutablePicoContainer pico = new JsonPicoContainer("banner.json");
Banner banner = (Banner)pico.getComponent("banner"); banner.setVisible(true); }
And here is the content of the file banner.json:
[ {key: textComp, impl: javax.swing.JTextArea, parameters: [{}]}, {key: banner, impl: com.picocontainer.gems.containers.Banner, parameters: [{key: textComp}, {value: "Hello, world"}, {key: size}, {key: bold}]}, {key: size, value: 36, type: int}, {key: bold, value: true, type: boolean}]

The JSON container has a dependency on google-gson, therefore it would belong to the gems. I've tested the container using the code from the picocontainer-git repository. (I assume that the development on Pico 2.x has been discontinued.)
Do you find such a JSON container useful? If yes, how can I contribute it?
Best regards,Serban
Paul Hammant
2013-12-13 19:10:01 UTC
Permalink
If you've got JUnit tests that shows it in use, feel free to donate it -
we're more than happy to facilitate other people's use-cases.

As it happens we're on the cusp of releasing PicoContainer 3, but you may
have a strong preference for doing this for PicoContainer 2.0, and we're
happy with cooperating with that agenda too. Patch-files for Pico2
(imperfect Subversion world). A Github fork (and pull request) for
PicoContainer 3, if your happy with an implicit delay on release.

- Paul
Post by Serban Iordache
Hi all,
I would like to contribute a pico container that populates itself by means
of a configuration file in JSON format.
public class Banner extends JFrame {
public Banner(JTextComponent textComp, String text, int size, boolean
bold) {
textComp.setText(text);
textComp.setFont(new Font("Arial", (bold ? Font.BOLD : Font.PLAIN),
size));
add(textComp);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
pack();
}
public static void main(String[] args) {
MutablePicoContainer pico = new DefaultPicoContainer();
pico.addComponent("textComp", JTextArea.class,
DefaultConstructorParameter.INSTANCE);
pico.addComponent("banner", Banner.class,
new ComponentParameter("textComp"),
new ConstantParameter("Hello, world!"),
new ComponentParameter("size"),
new ComponentParameter("bold"));
pico.addConfig("size", 36);
pico.addConfig("bold", true);
Banner banner = (Banner)pico.getComponent("banner");
banner.setVisible(true);
}
}
public static void main(String[] args) {
MutablePicoContainer pico = new JsonPicoContainer("banner.json");
Banner banner = (Banner)pico.getComponent("banner");
banner.setVisible(true);
}
[
{key: textComp, impl: javax.swing.JTextArea, parameters: [{}]},
{key: banner, impl: com.picocontainer.gems.containers.Banner,
bold}]},
{key: size, value: 36, type: int},
{key: bold, value: true, type: boolean}
]
The JSON container has a dependency on google-gson, therefore it would
belong to the gems. I've tested the container using the code from the
picocontainer-git repository. (I assume that the development on Pico 2.x
has been discontinued.)
Do you find such a JSON container useful? If yes, how can I contribute it?
Best regards,
Serban
Serban Iordache
2013-12-14 02:50:39 UTC
Permalink
I've just sent the pull request for PicoContainer 3.0.
Attached is the patch for PicoContainer 2.x.
Serban
Date: Fri, 13 Dec 2013 14:10:01 -0500
From: paul-POq8DFUn+***@public.gmane.org
To: dev-qxt/k92ZUMzOYGyH7mwfjrEhcVWVsK+***@public.gmane.org
Subject: Re: [picocontainer-dev] JSON Pico container

If you've got JUnit tests that shows it in use, feel free to donate it - we're more than happy to facilitate other people's use-cases.
As it happens we're on the cusp of releasing PicoContainer 3, but you may have a strong preference for doing this for PicoContainer 2.0, and we're happy with cooperating with that agenda too. Patch-files for Pico2 (imperfect Subversion world). A Github fork (and pull request) for PicoContainer 3, if your happy with an implicit delay on release.

- Paul
Paul Hammant
2013-12-25 20:38:05 UTC
Permalink
I've processed the pull-request, reworked it a little, and relocated it to
script/json/* (along side the Groovy, JRuby, XML ones).

There's a bunch of code that's not covered. Can you share any more of your
use cases, so that we could end up with more comprehensive tests?

Also, there's code in the json to say what type of parameter things are
(int, long, double etc). Is there another way we could work things such
that the declarations of that kind are not needed? PicoContainer has a
class called BuildInConverters that could auto convert strings to params,
and use the order of the parameter to make the conversion. It could be
that your use case is pretty strong, and this would be an additonal way of
marking up trees.

Also, take a look at the nesting of containers in
https://github.com/picocontainer/picocontainer/blob/master/booter/src/test/composition.groovyand
have a think about whether we could evolve the json grammar in that
*nested* direction. (see also
https://github.com/picocontainer/picocontainer/blob/master/script/script-groovy/src/test/com/picocontainer/script/groovy/GroovyNodeBuilderTestCase.javaand
https://github.com/picocontainer/picocontainer/blob/master/script/script-groovy/src/test/com/picocontainer/script/groovy/GroovyContainerBuilderTestCase.java
)

Great work Serban:)

-ph
Post by Serban Iordache
I've just sent the pull request for PicoContainer 3.0.
Attached is the patch for PicoContainer 2.x.
Serban
------------------------------
Date: Fri, 13 Dec 2013 14:10:01 -0500
Subject: Re: [picocontainer-dev] JSON Pico container
If you've got JUnit tests that shows it in use, feel free to donate it -
we're more than happy to facilitate other people's use-cases.
As it happens we're on the cusp of releasing PicoContainer 3, but you may
have a strong preference for doing this for PicoContainer 2.0, and we're
happy with cooperating with that agenda too. Patch-files for Pico2
(imperfect Subversion world). A Github fork (and pull request) for
PicoContainer 3, if your happy with an implicit delay on release.
- Paul
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
Loading...