I'm currently working on a new library called Dollar and I'd like to share some thoughts about it. It is pre-alpha software which means it's really in a prototyping state and the interfaces are not fixed yet.
If you like the ease of JavaScript, Ruby, Groovy etc. but also enjoy being able to work within the Java language then Dollar is for you. You can write typesafe code and then drop into typeless Dollar code whenever you need to. Dollar is both an alternative paradigm and a complementary resource.
Everything you see in this blogpost is 100% Java, no matter what it might look like at first glance. I guarantee it's all perfectly valid Java.
Show me the code!
So let's create some JSON in Java:
1
2
3
4
5
6
7
8
9
10
var profile = $(
$("name", "Neil"),
$("age", new Date().getYear() + 1900 - 1970),
$("gender", "male"),
$("projects", $jsonArray("snapito", "dollar")),
$("location",
$("city", "brighton"),
$("postcode", "bn1 6jj"),
$("number", 343)
));
As you can see, creating nested JSON data is incredibly easy.
You're probably wondering about var profile in the code above. var is the interface name of the dynamic typing systems. It was chosen to help the code read more like a dynamic language.
And then we can query that JSON using Dollar, in much the same way you would in jQuery.
1
String name = profile.$("name").$$();
Or we can do queries using JavaScript (Nashorn) expressions ($ means the current value).
1
2
profile.$("$['age']/11").$int()
profile.$("$.gender").$()
We can also create our objects from JSON Strings ...