Comments (7)
This is basically a case where the error handling could be stricter rather than
silently trying to iterate over a string.
The behavior you describe as expected isn't what's intended, see:
http://code.google.com/p/json-template/wiki/Reference
The .or section is only expanded if the section is empty or missing -- not if
it has
an item which isn't a list.
So I think we should just raise EvaluationError in case the node for a repeated
section isn't a list.
Original comment by [email protected]
on 28 May 2009 at 3:40
from json-template.
Ah, I interpreted the reference document slightly different and assumed the .or
section to be also executed if the intended target (a list) was not there.
Perhaps
that was just wishful thinking.
The use case for this is automatically generated JSON from an XML source. I
often get
the following XML:
<list>
<item>one</item>
<item>two</item>
</list>
The generated JSON then becomes:
{list: {item: ['one', 'two'] }}
Unfortunately, when the list has only one element, the resulting JSON is:
{list: {item: 'one'}}
I don't have much control (or rather, I don't want to) over the way the JSON is
generated as it is done automatically by an XSLT stylesheet from an XML source.
I was
hoping to catch this case in the template.
Original comment by [email protected]
on 28 May 2009 at 8:38
from json-template.
I see. Yeah it's unforunate that you have that type of output and don't have
control
over it.
But there are a lot of issues you can get with arbitrary JSON, and the general
solution is to modify the data dictionary (automatically). I just checked in a
Python example of this, in python/jsontemplate/datadict*.py.
It should be simple to do a similar thing from JavaScript for your case,
although
possibly issue 22 is relevant too.
Original comment by [email protected]
on 31 May 2009 at 7:49
from json-template.
Original comment by [email protected]
on 10 Jul 2009 at 6:31
- Changed title: Trying to iterate over a string with repeated section should throw an error (JavaScript version)
from json-template.
b.l.stein, what did you end up doing? I have the same issue; my JSON is
converted from XML, so sometimes a
value is an array of strings, sometimes it is a string.
I was thinking about converting certain strings to an array of a string (a
singleton) manually, but it's not the
nicest solution ...
Original comment by mikkelg
on 24 Jul 2009 at 1:28
from json-template.
I ended up writing formatter functions for some of my problem cases and
rewriting the
JSON in other cases (with a simple recursive function.)
Original comment by [email protected]
on 27 Jul 2009 at 4:05
from json-template.
Original comment by [email protected]
on 7 Nov 2009 at 9:25
- Added labels: Todo-JS
from json-template.
Related Issues (20)
- Unbalanced section push and pop in the Java implementation HOT 3
- Add a LISP-style "let" statement
- A predicate containing the text 'or' results in a template parsing error
- typo in UrlParamsJsonTemplateFormatter HOT 2
- Error in JsonTemplateModule::FromFile() HOT 1
- Iteration over dictionary - Patch for /python/jsontemplate/_jsontemplate.py HOT 1
- Iteration over dictionary - Patch for /python/jsontemplate/_jsontemplate.py HOT 1
- cannot pip install json-template
- Javascript within JSON templates HOT 1
- <img> src being ignored. HOT 1
- Possibly a typo introduced by revision cc10d7e78b94 HOT 1
- HTML formatter doesn't work for numeric values HOT 2
- expand sometime return blank HOT 1
- Patch for /jesyll-doc/__config.json
- token_re_cache in javascript implementation doesn't do anything? HOT 2
- Setup.py dependency chaining broken; fix enclosed HOT 1
- Python 3 diff HOT 1
- json data formte with metdata of object HOT 1
- Iteration over dictionary (patch) HOT 2
- saite de filmes
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json-template.