FWIW, I have thought about implementing something like this in the past, and
there are a few challenges. The first one you will run into is how to specify
an object's type. For example, consider this BXML which creates a Window with a
Label as its content:
<Window>
<content>
<Label text="Hello">
</content>
</Window>
Since "content" is Window's default property, this example can actually be
shortened to the following:
<Window>
<Label text="Hello">
</Window>
How would you specify this in JSON? You could try this:
{ "content": {
"text": "Hello"
}
}
But that doesn't tell you that the root object is a Window and the content
object is a Label. You'd need to extend the JSON syntax to support something
like this:
"Window": {
"content": "Label" {
"text": "Hello"
}
}
However, JSON doesn't support namespaces, so you might actually need to do
something like this (or further extend JSON to add namespace support):
"org.apache.pivot.wtk.Window": {
"content": "org.apache.pivot.wtk.Label" {
"text": "Hello"
}
}
That starts to look a lot more verbose (and a lot less readable) than the XML
version:
<Window>
<Label text="Hello">
</Window>
Note however that you can still use JSONSerializer to return typed objects. If
you pass a type argument to the JSONSerializer constructor, it will use
reflection to determine what to instantiate. The only limitation is that the
instantiated types cannot be abstract. For example, JSONSerializer cannot be
used to read the sample JSON shown above, since Window's content property is an
instance of the abstract Component class (there is no way for JSONSerializer to
know that it should create a Label vs. an arbitrary Component):
{ "content": {
"text": "Hello"
}
}
However, this technique is still pretty handy, particularly for deserializing
JSON into application-specific data objects.
Hope this helps,
G
On Nov 10, 2010, at 8:12 PM, Superstring Media wrote:
> I'm also interested in this.
>
> In my application I also have a related issue where I would use a BXML (or
> BJSON) file to create a graph of context objects where each context object is
> basically a container for a number of maps. I'd like to be able to include
> references to JSON files that contain key-value pairs that are then dumped
> into a given map in one of the context objects.
>
> Then I need to swap key-values from any number of alternate JSON files in/out
> of a given map in a context object. Likely BXML (or BJSON) would not be
> required for this because the graph of context object does not need to be
> rebuilt, only the key-values in given maps need to be swapped or have
> additional key-values added them.
>
> Thom
>
>
> On 2010-11-10, at 6:53 PM, Greg Brown wrote:
>
>> It isn't currently supported, though there's nothing to preclude you (or
>> someone) from writing such support.
>> G
>>
>> On Nov 10, 2010, at 6:33 PM, Christian Schlichtherle wrote:
>>
>>> Hi,
>>>
>>> I am new to Apache Pivot and try to find an answer to what I guess might be
>>> an FAQ – but I could not yet find an answer yet.
>>>
>>> So here is my question: Is it possible to declare my UI components using
>>> JSON instead of XML?
>>>
>>> I found the class JSONSerializer in the Javadoc for 1.5.2, but it doesn’t
>>> provide the same interface as WTKXSerializer, so I wonder if it’s possible.
>>>
>>> Why I’ld like to use it? Because I am not a particular fan of XML tag soup
>>> and I think it would be a great architectural feature if Apache Pivot would
>>> not only provide the freedom of choice of a UI scripting language
>>> (Jacascript, Groovy etc.), but also the freedom of choice of a UI component
>>> declaration language.
>>>
>>> I’m not sure if it’s feasible at all, however.
>>>
>>> Regards,
>>> Christian Schlichtherle
>>
>