Merge lp:~zorba-coders/zorba/feature-json_parser into lp:zorba

Proposed by Paul J. Lucas
Status: Superseded
Proposed branch: lp:~zorba-coders/zorba/feature-json_parser
Merge into: lp:zorba
Diff against target: 6752 lines (+5532/-72)
163 files modified
ChangeLog (+1/-0)
include/zorba/diagnostic.h (+4/-1)
include/zorba/internal/ztd.h (+2/-1)
include/zorba/pregenerated/diagnostic_list.h (+34/-0)
modules/com/zorba-xquery/www/modules/CMakeLists.txt (+4/-0)
modules/com/zorba-xquery/www/modules/converters/json-options.xsd (+60/-0)
modules/com/zorba-xquery/www/modules/converters/json.xq (+260/-0)
modules/com/zorba-xquery/www/modules/pregenerated/errors.xq (+69/-1)
src/context/static_context.cpp (+7/-0)
src/context/static_context.h (+1/-0)
src/diagnostics/diagnostic.cpp (+3/-0)
src/diagnostics/diagnostic_en.xml (+65/-0)
src/diagnostics/pregenerated/diagnostic_list.cpp (+51/-0)
src/diagnostics/pregenerated/dict_en.cpp (+19/-0)
src/diagnostics/qname.cpp (+6/-0)
src/functions/library.cpp (+2/-0)
src/functions/pregenerated/func_json.cpp (+87/-0)
src/functions/pregenerated/func_json.h (+79/-0)
src/functions/pregenerated/function_enum.h (+2/-0)
src/runtime/CMakeLists.txt (+3/-0)
src/runtime/full_text/ft_match.cpp (+2/-3)
src/runtime/json/common.cpp (+62/-0)
src/runtime/json/common.h (+120/-0)
src/runtime/json/json_impl.cpp (+217/-0)
src/runtime/json/jsonml_array.cpp (+285/-0)
src/runtime/json/jsonml_array.h (+41/-0)
src/runtime/json/pregenerated/json.cpp (+94/-0)
src/runtime/json/pregenerated/json.h (+114/-0)
src/runtime/json/snelson.cpp (+515/-0)
src/runtime/json/snelson.h (+41/-0)
src/runtime/spec/json/json.xml (+52/-0)
src/runtime/spec/mappings.xml (+4/-0)
src/runtime/visitors/pregenerated/planiter_visitor.h (+10/-0)
src/runtime/visitors/pregenerated/printer_visitor.cpp (+29/-0)
src/runtime/visitors/pregenerated/printer_visitor.h (+6/-0)
src/unit_tests/CMakeLists.txt (+2/-0)
src/unit_tests/json_parser.cpp (+636/-0)
src/unit_tests/unit_test_list.h (+1/-0)
src/unit_tests/unit_tests.cpp (+1/-0)
src/util/CMakeLists.txt (+2/-0)
src/util/json_parser.cpp (+662/-0)
src/util/json_parser.h (+570/-0)
src/util/mem_streambuf.cpp (+119/-0)
src/util/mem_streambuf.h (+108/-0)
src/util/omanip.h (+205/-57)
src/util/oseparator.h (+17/-5)
src/util/stl_util.h (+51/-2)
src/util/string_util.h (+2/-1)
test/rbkt/ExpQueryResults/zorba/json/json-jsonml_array-parse-01.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-jsonml_array-parse-02.xml.res (+4/-0)
test/rbkt/ExpQueryResults/zorba/json/json-jsonml_array-parse-03.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-jsonml_array-parse-04.xml.res (+7/-0)
test/rbkt/ExpQueryResults/zorba/json/json-jsonml_array-parse-05.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-jsonml_array-parse-wikipedia.xml.res (+10/-0)
test/rbkt/ExpQueryResults/zorba/json/json-jsonml_array-serialize-01.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-jsonml_array-serialize-02.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-jsonml_array-serialize-03.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-jsonml_array-serialize-indent-wikipedia.xml.res (+11/-0)
test/rbkt/ExpQueryResults/zorba/json/json-jsonml_array-serialize-none-wikipedia.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-jsonml_array-serialize-some-wikipedia.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-null-handling.xml.res (+3/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-numbers-and-decimals.xml.res (+7/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-parse-array-01.xml.res (+3/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-parse-array-02.xml.res (+3/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-parse-array-03.xml.res (+3/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-parse-array-04.xml.res (+3/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-parse-array-05.xml.res (+4/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-parse-array-06.xml.res (+8/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-parse-array-07.xml.res (+10/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-parse-array-08.xml.res (+6/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-parse-example.xml.res (+14/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-parse-object-01.xml.res (+3/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-parse-object-02.xml.res (+4/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-parse-object-03.xml.res (+8/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-parse-object-04.xml.res (+6/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-parse-serialize.xml.res (+3/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-serialize-array-01.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-serialize-array-02.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-serialize-array-03.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-serialize-array-04.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-serialize-array-05.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-serialize-array-12.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-serialize-indent-example.xml.res (+11/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-serialize-none-example.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-serialize-object-01.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-serialize-object-02.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-serialize-object-03.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-serialize-parse.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-serialize-some-example.xml.res (+1/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-uncommon-chars.xml.res (+3/-0)
test/rbkt/ExpQueryResults/zorba/json/json-snelson-utf-8.xml.res (+3/-0)
test/rbkt/Queries/zorba/json/json-invalid-option-parameter.spec (+1/-0)
test/rbkt/Queries/zorba/json/json-invalid-option-parameter.xq (+10/-0)
test/rbkt/Queries/zorba/json/json-jsonml_array-parse-01.xq (+9/-0)
test/rbkt/Queries/zorba/json/json-jsonml_array-parse-02.xq (+14/-0)
test/rbkt/Queries/zorba/json/json-jsonml_array-parse-03.xq (+18/-0)
test/rbkt/Queries/zorba/json/json-jsonml_array-parse-04.xq (+27/-0)
test/rbkt/Queries/zorba/json/json-jsonml_array-parse-05.xq (+66/-0)
test/rbkt/Queries/zorba/json/json-jsonml_array-parse-06.spec (+1/-0)
test/rbkt/Queries/zorba/json/json-jsonml_array-parse-06.xq (+10/-0)
test/rbkt/Queries/zorba/json/json-jsonml_array-parse-wikipedia.xq (+23/-0)
test/rbkt/Queries/zorba/json/json-jsonml_array-serialize-01.xq (+10/-0)
test/rbkt/Queries/zorba/json/json-jsonml_array-serialize-02.xq (+13/-0)
test/rbkt/Queries/zorba/json/json-jsonml_array-serialize-03.xq (+10/-0)
test/rbkt/Queries/zorba/json/json-jsonml_array-serialize-indent-wikipedia.xq (+20/-0)
test/rbkt/Queries/zorba/json/json-jsonml_array-serialize-none-wikipedia.xq (+19/-0)
test/rbkt/Queries/zorba/json/json-jsonml_array-serialize-some-wikipedia.xq (+20/-0)
test/rbkt/Queries/zorba/json/json-snelson-invalid-json.spec (+1/-0)
test/rbkt/Queries/zorba/json/json-snelson-invalid-json.xq (+9/-0)
test/rbkt/Queries/zorba/json/json-snelson-null-handling.xq (+5/-0)
test/rbkt/Queries/zorba/json/json-snelson-numbers-and-decimals.xq (+10/-0)
test/rbkt/Queries/zorba/json/json-snelson-parse-array-01.xq (+5/-0)
test/rbkt/Queries/zorba/json/json-snelson-parse-array-02.xq (+5/-0)
test/rbkt/Queries/zorba/json/json-snelson-parse-array-03.xq (+5/-0)
test/rbkt/Queries/zorba/json/json-snelson-parse-array-04.xq (+5/-0)
test/rbkt/Queries/zorba/json/json-snelson-parse-array-05.xq (+5/-0)
test/rbkt/Queries/zorba/json/json-snelson-parse-array-06.xq (+7/-0)
test/rbkt/Queries/zorba/json/json-snelson-parse-array-07.xq (+5/-0)
test/rbkt/Queries/zorba/json/json-snelson-parse-array-08.xq (+7/-0)
test/rbkt/Queries/zorba/json/json-snelson-parse-empty.spec (+1/-0)
test/rbkt/Queries/zorba/json/json-snelson-parse-empty.xq (+5/-0)
test/rbkt/Queries/zorba/json/json-snelson-parse-example.xq (+20/-0)
test/rbkt/Queries/zorba/json/json-snelson-parse-object-01.xq (+5/-0)
test/rbkt/Queries/zorba/json/json-snelson-parse-object-02.xq (+5/-0)
test/rbkt/Queries/zorba/json/json-snelson-parse-object-03.xq (+7/-0)
test/rbkt/Queries/zorba/json/json-snelson-parse-object-04.xq (+12/-0)
test/rbkt/Queries/zorba/json/json-snelson-parse-serialize.xq (+8/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-01.xq (+8/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-02.xq (+8/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-03.xq (+8/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-04.xq (+8/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-05.xq (+9/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-06.spec (+1/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-06.xq (+8/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-07.spec (+1/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-07.xq (+8/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-08.spec (+1/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-08.xq (+8/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-09.spec (+1/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-09.xq (+8/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-10.spec (+1/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-10.xq (+8/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-11.spec (+1/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-11.xq (+8/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-array-12.xq (+11/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-comment-node.spec (+1/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-comment-node.xq (+5/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-indent-example.xq (+24/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-invalid-value-for-attribute.spec (+1/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-invalid-value-for-attribute.xq (+8/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-none-example.xq (+19/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-object-01.xq (+8/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-object-02.xq (+9/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-object-03.xq (+12/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-parse.xq (+5/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-some-example.xq (+24/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-type-value-missing.spec (+1/-0)
test/rbkt/Queries/zorba/json/json-snelson-serialize-type-value-missing.xq (+8/-0)
test/rbkt/Queries/zorba/json/json-snelson-uncommon-chars.xq (+5/-0)
test/rbkt/Queries/zorba/json/json-snelson-utf-8.xq (+5/-0)
test/unit/CMakeLists.txt (+1/-0)
test/update/Queries/zorba/store/sc3.spec (+2/-0)
test/update/Queries/zorba/store/sc3_ex3.xq (+1/-1)
To merge this branch: bzr merge lp:~zorba-coders/zorba/feature-json_parser
Reviewer Review Type Date Requested Status
William Candillon Needs Fixing
Paul J. Lucas Approve
Sorin Marian Nasoi Pending
Matthias Brantner Pending
Review via email: mp+91959@code.launchpad.net

This proposal supersedes a proposal from 2012-02-02.

This proposal has been superseded by a proposal from 2012-02-14.

Description of the change

New JSON parser and module.
Fixed the missing quote in the documentation.
Added checking of stream state for manipulators.
All bugs fixed.
Yet another bug fixed.

To post a comment you must log in.
Revision history for this message
Sorin Marian Nasoi (sorin.marian.nasoi) wrote : Posted in a previous version of this proposal

I have resubmitted the merge proposal after committing the fix for the JSON tests.

Revision history for this message
William Candillon (wcandillon) wrote : Posted in a previous version of this proposal

The documentation is missing some examples.
The example section at http://docs.basex.org/wiki/JSON_Module is a good place to get inspired.

Why A chars have a backslash in error code descriptions:
ZJSE0001 if \a $xml is not a document or element node.
ZJSE0002 if \a $xml contains an element that is missing a required attribute.
ZJSE0003 if \a $xml contains an attribute having an illegal value.
ZJSE0004 if \a $xml contains an illegal element.
ZJSE0005 if \a $xml contains an illegal child element for a JSON type.
ZJSE0006 if \a $xml contains an illegal child element.
ZJSE0007 if \a $xml contains an illegal text node.
ZJSE0008 if \a $xml contains an illegal value for a JSON type.

review: Needs Fixing
Revision history for this message
Paul J. Lucas (paul-lucas) wrote : Posted in a previous version of this proposal

The \a is the doxygen way to say the following thing is an argument and should be italicized.

Revision history for this message
William Candillon (wcandillon) wrote : Posted in a previous version of this proposal

Doesn't seem to work for xqdoc

Revision history for this message
Paul J. Lucas (paul-lucas) : Posted in a previous version of this proposal
review: Approve
Revision history for this message
William Candillon (wcandillon) wrote : Posted in a previous version of this proposal

The module works great.
There is room for improvement regarding its documentation.
"There are many ways to represent JSON data in XML" -> There are two ways to represent JSON data in XML:
- bullet 1 (John Snelson's format)
- bullet 2 (JSON)
Then add two examples, you can take some inspiration at http://docs.basex.org/wiki/JSON_Module

For parse#1, add a small example.
For parse#2, add two small examples and some description of what is excepted for the option element (which namespace, example of schema instance instance)
For serialize#1, please elaborate on what the following means: "Serializes an XDM into JSON using one of the representations described above." and add two small examples.
For serialize#2, same comment than for serialize#1 + add some explanation of what is excepted for the option element.

review: Needs Fixing
Revision history for this message
Paul J. Lucas (paul-lucas) wrote : Posted in a previous version of this proposal

No, the statement "There are many ways to represent JSON data in XML" is correct as it is. There *are* *many* ways: Zorba only implements *two* of those *many* ways.

Adding *two* examples is too much. API documentation isn't supposed to be the primary documentation.

Revision history for this message
Matthias Brantner (matthias-brantner) wrote : Posted in a previous version of this proposal

I think William is right. We should have a couple of examples which help the user to get started with this module. Those could either be inline in the text or links generated using the @example xqdoc tag.

The serialize functions should be annotated %ann:streamable because they return a streamable string (see modules/com/zorba-xquery/www/modules/fetch.xq).

Some error codes lack a prefix (e.g. ZJPE0001). If I understood correctly, it should be zerr:ZJPE0001. Otherwise, the user doesn't know how to catch the error.

Under which circumstances does json:parse return the empty sequence? What does json:serialize return if the input is the empty sequence?

review: Needs Fixing
Revision history for this message
Sorin Marian Nasoi (sorin.marian.nasoi) wrote : Posted in a previous version of this proposal

1) Should be possible to call json:serialize(json:parse(VALID_JSON))
where VALID_JSON is any valid JSON string
see failing test test/rbkt/zorba/json/json-snelson-serialize-parse
added bug lp:920717

2) array and object closed prematurely in json:parse
see failing tests test/rbkt/zorba/json/json-snelson-parse-array-06 and test/rbkt/zorba/json/json-snelson-parse-array-07
added bug lp:920719

3) When trying to parsing a valid JSON string with invalid JSON option parameter the error raised is:
http://www.w3.org/2005/xqt-errors:XPST0003
Please add a more useful error message: see failing test test/rbkt/zorba/json/json-invalid-option-parameter
added bug lp:920720

4) Parsing an empty value has wrong behavior for both JSON mappings (Snelson and JSON-ML).
Please see failing tests:
- test/rbkt/zorba/json/json-snelson-empty-value
- test/rbkt/zorba/json/json-jsonml-empty-value
added bug lp:920721

5) Comment nodes should be ignored by json:serialize
Please see failing tests:
- test/rbkt/zorba/json/json-snelson-serialize-object-03
- test/rbkt/zorba/json/json-snelson-serialize-array-12
added bug lp:920722

6) XQDoc issues:
- there is no @project tag for the new json.xq module: as a result, in the XQDoc documentation is generated in the www.zorba-xquery.com/modules/converters instead of data processing/data converters
Please add a @project data processing/data converters in the module description in order to fix this.

- first parse function does not state
zerr:ZJPE0006 as a possible error condition if the passes JSON string is invalid
See added test json-snelson-invalid-json.xq
Added bug lp:920724

review: Needs Fixing
Revision history for this message
Sorin Marian Nasoi (sorin.marian.nasoi) wrote : Posted in a previous version of this proposal

r10618 contains all the tests mentioned above.

The tests are passing because they are marked as EXPECTED_FAILURES.

Revision history for this message
Paul J. Lucas (paul-lucas) wrote : Posted in a previous version of this proposal

Why did you mark them as EXPECTED_FAILURE if they're not expected to fail? Presumably, you *want* them fixed, right?

Revision history for this message
Chris Hillery (ceejatec) wrote : Posted in a previous version of this proposal

EXPECTED_FAILURE does *not* mean "negative test". It means "this is broken, we know it's broken, and we're working on it". That's why you have to supply a bug number to the macro. It probably should be renamed "KNOWN_BUG".

Revision history for this message
Paul J. Lucas (paul-lucas) wrote : Posted in a previous version of this proposal

It means "known bug" for things that are in the trunk. If it's on a branch (as this is), then either (1) it will be fixed before it's merged into the trunk (at which point the EXPECTED_FAILURE will have to be removed since it will no longer be failing -- which begs my question of, "Why put it in in the first place?") or (2) the test will be deemed invalid and removed.

Revision history for this message
Chris Hillery (ceejatec) wrote : Posted in a previous version of this proposal

Hmm... valid points. I think there's some value in using EXPECTED_FAILURE() anyway, since it documents the relationship to new bugs that are filed.

It does introduce the possibility of unintentionally merging a new bug onto the trunk, but the diff will clearly show a new EXPECTED_FAILURE() marker and I would hope that any reviewer would raise a serious question about that.

I guess I would say that it's probably unnecessary to add EXPECTED_FAILURE()s (and associated bugs) for small review comments. However, for anything which might reasonably take more than a few hours to fix, IMHO it's probably good practice just to help ensure that no issues get forgotten. In general I would leave that decision up to the team working on the branch in question, though.

Revision history for this message
Sorin Marian Nasoi (sorin.marian.nasoi) wrote : Posted in a previous version of this proposal

> It means "known bug" for things that are in the trunk. If it's on a branch
> (as this is), then either (1) it will be fixed before it's merged into the
> trunk (at which point the EXPECTED_FAILURE will have to be removed since it
> will no longer be failing -- which begs my question of, "Why put it in in the
> first place?") or (2) the test will be deemed invalid and removed.

Paul, IMO:
- adding some bugs on Launchpad
- adding tests that fail and marking them as KNOWN_ISSUE (a.k.a. EXPECTED_FAILURES)
was far better than adding a comment in your merge proposal.

Keep in mind that I have spent my time in order to give you a hand in pointing out the issues I found.

Adding separate bugs allows us to:
- discussed the raised issues separately
- make commits to fix them separately into the branch

And to prove my point: you already wrote 2 comments *without* even looking over *any* of the issues I have raised.

Anyway, I already committed a fix in the branch for bug lp:920724.

Revision history for this message
Paul J. Lucas (paul-lucas) wrote : Posted in a previous version of this proposal

Chris Hillery wrote: "I think there's some value in using EXPECTED_FAILURE() anyway, since it documents the relationship to new bugs that are filed."

The relationship to new bugs *from* _______?

Revision history for this message
Chris Hillery (ceejatec) wrote : Posted in a previous version of this proposal

Documents the relationship of the newly-added failing tests to the bugs tracking those issues.

Revision history for this message
Paul J. Lucas (paul-lucas) wrote : Posted in a previous version of this proposal

IMO, you didn't *need* to add any comment to the merge proposal other than "Bugs filed" -- I can read the bugs myself.

Sorin wrote: "Keep in mind that I have spent my time in order to give you a hand in pointing out the issues I found."

It's all of our job to review each others' code under the "New Order" so please don't act like it was a personal favor.

I never questioned your adding separate bugs. For the record, I *like* separate bugs. I *only* questioned why you added EXPECTED_FAILURE for those bugs that, as I've pointed out, is unnecessary and not only creates more work for you (because you put them in) but more work for me (because I have to take them out).

Sorin wrote: "And to prove my point: you already wrote 2 comments *without* even looking over *any* of the issues I have raised."

*My* point has *nothing* to do with any bugs in particular. Chris understands my point. Do you?

Revision history for this message
Paul J. Lucas (paul-lucas) wrote : Posted in a previous version of this proposal

@Chris: Hmmm... again, because this is a branch and no Zorba user will ever see this stuff, it's at best only marginally useful.

Revision history for this message
Sorin Marian Nasoi (sorin.marian.nasoi) wrote : Posted in a previous version of this proposal

Paul, let me ask you something: are you sure you are not missing the point?

IMHO the point is to fix the issues that were raised ASAP.

I have spent my time in order to give as many details as possible in order to fix them ASAP.
Also I have committed a fix in the branch for one of the opened bugs.

Please look over the bugs and let's try to fix them in order to commit the new improved JSON 2.0 module to the trunk.

If you think that the decision I took while reviewing your merge deserves further discussion let's put it on the next weekly conference agenda.

Revision history for this message
Sorin Marian Nasoi (sorin.marian.nasoi) wrote : Posted in a previous version of this proposal

> IMO, you didn't *need* to add any comment to the merge proposal other than
> "Bugs filed" -- I can read the bugs myself.
FYI: Matthias asked me (in a separate email discussion) to add a small review of the issues I fount in the merge proposal: please take this issue with him.

> Sorin wrote: "Keep in mind that I have spent my time in order to give you a
> hand in pointing out the issues I found."
>
> It's all of our job to review each others' code under the "New Order" so
> please don't act like it was a personal favor.
OK, next time I will review your code I will simply add a one sentence in the merge proposal and keep adding comments because you clearly think this is better.

> I never questioned your adding separate bugs. For the record, I *like*
> separate bugs. I *only* questioned why you added EXPECTED_FAILURE for those
> bugs that, as I've pointed out, is unnecessary and not only creates more work
> for you (because you put them in) but more work for me (because I have to take
> them out).
I will never do this again.

> Sorin wrote: "And to prove my point: you already wrote 2 comments *without*
> even looking over *any* of the issues I have raised."
>
> *My* point has *nothing* to do with any bugs in particular. Chris understands
> my point. Do you?
No, I do not. IMHO you are missing the point, that is: let's try to fix the *bugs* and discuss the procedures later.
Do you understand my point?

Revision history for this message
Paul J. Lucas (paul-lucas) wrote : Posted in a previous version of this proposal

No, the point is *not* to fix this issues ASAP. The feature is not a high-priority feature, so there's no reason to do this ASAP. My *only* point was why you added EXPECTED_FAILURE lines to the CMakeLists.txt file -- that's it.

But it's moot now since I've removed all the new EXPECTED_FAILURES that you've added. They're of no use to me. Now when I run the test suite and it reports 100% tests passed, I can be confident that I've fixed all the bugs and not have an errant EXPECTED_FAILURE in there.

In the future, please don't add EXPECTED_FAILURE lines to code on a *branch* -- thanks.

Revision history for this message
Sorin Marian Nasoi (sorin.marian.nasoi) wrote : Posted in a previous version of this proposal

> No, the point is *not* to fix this issues ASAP. The feature is not a high-
> priority feature, so there's no reason to do this ASAP. My *only* point was
> why you added EXPECTED_FAILURE lines to the CMakeLists.txt file -- that's it.
>
> But it's moot now since I've removed all the new EXPECTED_FAILURES that you've
> added. They're of no use to me. Now when I run the test suite and it reports
> 100% tests passed, I can be confident that I've fixed all the bugs and not
> have an errant EXPECTED_FAILURE in there.
>
> In the future, please don't add EXPECTED_FAILURE lines to code on a *branch*
> -- thanks.
My 2 cents: no matter what the brief description of the "ctest -R SOME_TEST" shows, one should *always* check out the details inside the ctest logs.

http://en.wikipedia.org/wiki/The_Devil_is_in_the_details

Revision history for this message
Paul J. Lucas (paul-lucas) : Posted in a previous version of this proposal
review: Approve
Revision history for this message
William Candillon (wcandillon) wrote : Posted in a previous version of this proposal

I believe that bug #920717 isn't fixed.

The intro has two examples of XML.
For each of the XML, you should add the associated JSON.

Would it be possible to get all the bug reports that Sorin created associated to this merge request?

review: Needs Fixing
Revision history for this message
Paul J. Lucas (paul-lucas) :
review: Approve
Revision history for this message
Paul J. Lucas (paul-lucas) wrote :

It was an unrelated bug -- fixed.

Revision history for this message
William Candillon (wcandillon) wrote :

This merge request is pending on bug #920717

Also there are some documentation issues that have not been addressed yet.
How do you we do this?
Do you make another shot at it first or we should have call with Matthias to discuss how to review it?

review: Needs Fixing
Revision history for this message
William Candillon (wcandillon) wrote :

Do you take another shot at it first or should we have a call with Matthias to discuss how to improve the module documentation?*

Revision history for this message
Paul J. Lucas (paul-lucas) wrote :

If you want the documentation fixed, you need to specify exactly what needs fixing -- I'm not a mind-reader.

10641. By Paul J. Lucas

Yet another bug fixed.

10642. By Paul J. Lucas

Merge from trunk.

Revision history for this message
William Candillon (wcandillon) wrote :

"Zorba implements that proposed by John Snelson." -> is that correct english? I'm not sure.
For John Snelson representation example, please add the corresponding JSON and XQuery function call above.
For the JSONML example, please add the corresponding JSON and XQuery function call above.

I think that this fix will dramatically improve the time needed for someone to get started with the module.

In parse#1, add a small example.
"Returns: said XDM instance." is that correct?

In parse#2, add a small example.
Add a description of how to build element(json-options:options)

In serialize#1, add a small example.

In serialize#2, add a small example.
Add a description of how to build element(json-options:options)

Revision history for this message
William Candillon (wcandillon) wrote :

I have reopened bug #920717.
This merge is still pending on the resolution of this bug.

10643. By Paul J. Lucas

Merge from trunk.

Revision history for this message
Paul J. Lucas (paul-lucas) wrote :

> "Zorba implements that proposed by John Snelson." -> is that correct english?
> I'm not sure.

Yes.

> In parse#1, add a small example.
> "Returns: said XDM instance." is that correct?

Yes.

> In parse#2, add a small example.
> Add a description of how to build element(json-options:options)

I would except XQDoc is broken and renders it very badly.

> In serialize#1, add a small example.
> In serialize#2, add a small example.

It's just the reverse of what's above!

10644. By Paul J. Lucas

More documentation.

10645. By Paul J. Lucas

Merge from trunk.

Revision history for this message
William Candillon (wcandillon) wrote :

If the documentation is correct in the source, I will fix everything else.

10646. By Paul J. Lucas

Added yet more examples to JSON documentation.

10647. By Paul J. Lucas

Clean-up; new test.

10648. By Paul J. Lucas

Fixed bug 930573.

10649. By Paul J. Lucas

Merge from trunk.

10650. By Paul J. Lucas

Removed data-converters tag.

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ChangeLog'
2--- ChangeLog 2012-02-10 01:01:16 +0000
3+++ ChangeLog 2012-02-14 03:46:19 +0000
4@@ -36,6 +36,7 @@
5 * Fixed bug #918211 (xqueryx fulltext w3c conformance generation)
6 * Fixed bug #918157 (Add XQFTTS to validation queue)
7 * Fixed bug with unversioned modules with C++ external functions
8+ * Fixed bug #878508 (JSON Module not escaping escape characters)
9
10 version 2.1
11
12
13=== modified file 'include/zorba/diagnostic.h'
14--- include/zorba/diagnostic.h 2011-07-01 16:07:54 +0000
15+++ include/zorba/diagnostic.h 2012-02-14 03:46:19 +0000
16@@ -239,7 +239,10 @@
17 ZORBA_DEBUGGER, // Zorba Debugger
18 ZORBA_OS, // Operating System
19 ZORBA_SERIALIZATION,
20- ZORBA_STORE
21+ ZORBA_STORE,
22+
23+ JSON_PARSER,
24+ JSON_SERIALIZATION
25 };
26
27 /**
28
29=== modified file 'include/zorba/internal/ztd.h'
30--- include/zorba/internal/ztd.h 2011-08-23 13:32:16 +0000
31+++ include/zorba/internal/ztd.h 2012-02-14 03:46:19 +0000
32@@ -355,7 +355,8 @@
33 template<typename T> inline
34 typename std::enable_if<ZORBA_TR1_NS::is_pointer<T>::value,std::string>::type
35 to_string( T p ) {
36- return p ? to_string( *p ) : "<null>";
37+ typedef typename ZORBA_TR1_NS::remove_pointer<T>::type const* T_const_ptr;
38+ return p ? to_string( *static_cast<T_const_ptr>( p ) ) : "<null>";
39 }
40
41 /**
42
43=== modified file 'include/zorba/pregenerated/diagnostic_list.h'
44--- include/zorba/pregenerated/diagnostic_list.h 2011-12-21 14:40:33 +0000
45+++ include/zorba/pregenerated/diagnostic_list.h 2012-02-14 03:46:19 +0000
46@@ -746,6 +746,40 @@
47
48 extern ZORBA_DLL_PUBLIC ZorbaErrorCode XSST0010;
49
50+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJPE0001_ILLEGAL_CHARACTER;
51+
52+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJPE0002_ILLEGAL_CODEPOINT;
53+
54+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJPE0003_ILLEGAL_ESCAPE;
55+
56+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJPE0004_ILLEGAL_LITERAL;
57+
58+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJPE0005_ILLEGAL_NUMBER;
59+
60+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJPE0006_UNEXPECTED_TOKEN;
61+
62+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJPE0007_UNTERMINATED_STRING;
63+
64+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJPE0008_ILLEGAL_QNAME;
65+
66+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJPE0009_ILLEGAL_EMPTY_STRING;
67+
68+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJSE0001_NOT_DOCUMENT_OR_ELEMENT_NODE;
69+
70+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJSE0002_ELEMENT_MISSING_ATTRIBUTE;
71+
72+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJSE0003_BAD_ATTRIBUTE_VALUE;
73+
74+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJSE0004_BAD_ELEMENT;
75+
76+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJSE0005_BAD_CHILD_ELEMENT;
77+
78+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJSE0006_NO_ELEMENT_CHILD;
79+
80+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJSE0007_NO_TEXT_CHILD;
81+
82+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZJSE0008_BAD_VALUE;
83+
84 } // namespace zerr
85
86 namespace zwarn {
87
88=== modified file 'modules/com/zorba-xquery/www/modules/CMakeLists.txt'
89--- modules/com/zorba-xquery/www/modules/CMakeLists.txt 2012-02-05 12:31:20 +0000
90+++ modules/com/zorba-xquery/www/modules/CMakeLists.txt 2012-02-14 03:46:19 +0000
91@@ -71,6 +71,10 @@
92 # Subdirectories
93 DECLARE_ZORBA_MODULE(FILE converters/base64.xq VERSION 2.0
94 URI "http://www.zorba-xquery.com/modules/converters/base64")
95+DECLARE_ZORBA_MODULE(FILE converters/json.xq VERSION 2.0
96+ URI "http://www.zorba-xquery.com/modules/converters/json")
97+DECLARE_ZORBA_SCHEMA(FILE converters/json-options.xsd
98+ URI "http://www.zorba-xquery.com/modules/converters/json-options")
99 DECLARE_ZORBA_MODULE(FILE introspection/sctx.xq VERSION 2.0
100 URI "http://www.zorba-xquery.com/modules/introspection/sctx")
101 DECLARE_ZORBA_MODULE(FILE xqdoc2xhtml/error.xq VERSION 2.0
102
103=== added file 'modules/com/zorba-xquery/www/modules/converters/json-options.xsd'
104--- modules/com/zorba-xquery/www/modules/converters/json-options.xsd 1970-01-01 00:00:00 +0000
105+++ modules/com/zorba-xquery/www/modules/converters/json-options.xsd 2012-02-14 03:46:19 +0000
106@@ -0,0 +1,60 @@
107+<!--
108+ ! Copyright 2006-2008 The FLWOR Foundation.
109+ !
110+ ! Licensed under the Apache License, Version 2.0 (the "License");
111+ ! you may not use this file except in compliance with the License.
112+ ! You may obtain a copy of the License at
113+ !
114+ ! http://www.apache.org/licenses/LICENSE-2.0
115+ !
116+ ! Unless required by applicable law or agreed to in writing, software
117+ ! distributed under the License is distributed on an "AS IS" BASIS,
118+ ! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
119+ ! See the License for the specific language governing permissions and
120+ ! limitations under the License.
121+-->
122+
123+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
124+ xmlns:json="http://www.zorba-xquery.com/modules/converters/json-options"
125+ targetNamespace="http://www.zorba-xquery.com/modules/converters/json-options"
126+ elementFormDefault="qualified"
127+ attributeFormDefault="unqualified">
128+
129+ <xs:element name="options">
130+ <xs:complexType>
131+ <xs:all>
132+
133+ <xs:element name="json-format" minOccurs="1" maxOccurs="1">
134+ <xs:complexType>
135+ <xs:attribute name="value" use="required">
136+ <xs:simpleType>
137+ <xs:restriction base="xs:string">
138+ <xs:enumeration value="Snelson"/>
139+ <xs:enumeration value="JsonML-array"/>
140+ <xs:enumeration value="JsonML-object"/>
141+ </xs:restriction>
142+ </xs:simpleType>
143+ </xs:attribute>
144+ </xs:complexType>
145+ </xs:element>
146+
147+ <xs:element name="whitespace" minOccurs="0" maxOccurs="1">
148+ <xs:complexType>
149+ <xs:attribute name="value" use="required">
150+ <xs:simpleType>
151+ <xs:restriction base="xs:string">
152+ <xs:enumeration value="none"/>
153+ <xs:enumeration value="some"/>
154+ <xs:enumeration value="indent"/>
155+ </xs:restriction>
156+ </xs:simpleType>
157+ </xs:attribute>
158+ </xs:complexType>
159+ </xs:element>
160+
161+ </xs:all>
162+ </xs:complexType>
163+ </xs:element>
164+</xs:schema>
165+
166+<!-- vim:set et sw=2 ts=2: -->
167
168=== added file 'modules/com/zorba-xquery/www/modules/converters/json.xq'
169--- modules/com/zorba-xquery/www/modules/converters/json.xq 1970-01-01 00:00:00 +0000
170+++ modules/com/zorba-xquery/www/modules/converters/json.xq 2012-02-14 03:46:19 +0000
171@@ -0,0 +1,260 @@
172+(:
173+ : Copyright 2006-2009 The FLWOR Foundation.
174+ :
175+ : Licensed under the Apache License, Version 2.0 (the "License");
176+ : you may not use this file except in compliance with the License.
177+ : You may obtain a copy of the License at
178+ :
179+ : http://www.apache.org/licenses/LICENSE-2.0
180+ :
181+ : Unless required by applicable law or agreed to in writing, software
182+ : distributed under the License is distributed on an "AS IS" BASIS,
183+ : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
184+ : See the License for the specific language governing permissions and
185+ : limitations under the License.
186+ :)
187+
188+xquery version "3.0";
189+
190+ (:~
191+ : Using this module, you can parse JSON data into XML, manipulate it like any
192+ : other XML data using XQuery, and serialize the result back as JSON.
193+ :
194+ : There are many ways to represent JSON data in XML, some loss-less ("round
195+ : tripable") and some lossy ("one way"). Loss-less representations preserve
196+ : the JSON data types <i>boolean</i>, <i>number</i>, and <i>null</i>; lossy
197+ : representations convert all data to strings.
198+ : <p/>
199+ : For a loss-less representation, Zorba implements that proposed by
200+ : <a href="http://john.snelson.org.uk/parsing-json-into-xquery">John Snelson</a>.
201+ : For example:
202+ : <pre>
203+ : {
204+ : "firstName" : "John",
205+ : "lastName" : "Smith",
206+ : "address" : {
207+ : "streetAddress" : "21 2nd Street",
208+ : "city" : "New York",
209+ : "state" : "NY",
210+ : "postalCode" : 10021
211+ : },
212+ : "phoneNumbers" : [ "212 732-1234", "646 123-4567" ]
213+ : }
214+ : </pre>
215+ : would be represented as:
216+ : <pre>
217+ : &lt;json type="object"&gt;
218+ : &lt;pair name="firstName" type="string"&gt;John&lt;/pair&gt;
219+ : &lt;pair name="lastName" type="string"&gt;Smith&lt;/pair&gt;
220+ : &lt;pair name="address" type="object"&gt;
221+ : &lt;pair name="streetAddress" type="string"&gt;21 2nd Street&lt;/pair&gt;
222+ : &lt;pair name="city" type="string"&gt;New York&lt;/pair&gt;
223+ : &lt;pair name="state" type="string"&gt;NY&lt;/pair&gt;
224+ : &lt;pair name="postalCode" type="number"&gt;10021&lt;/pair&gt;
225+ : &lt;/pair&gt;
226+ : &lt;pair name="phoneNumbers" type="array"&gt;
227+ : &lt;item type="string"&gt;212 732-1234&lt;/item&gt;
228+ : &lt;item type="string"&gt;646 123-4567&lt;/item&gt;
229+ : &lt;/pair&gt;
230+ : &lt;/json&gt;
231+ : </pre>
232+ : For a lossy representation, Zorba implements
233+ : <a href="http://jsonml.org/">JsonML</a> (the array form).
234+ : For example:
235+ : <pre>
236+ : [ "person",
237+ : { "created" : "2006-11-11T19:23",
238+ : "modified" : "2006-12-31T23:59" },
239+ : [ "firstName", "Robert" ],
240+ : [ "lastName", "Smith" ],
241+ : [ "address",
242+ : { "type" : "home" },
243+ : [ "street", "12345 Sixth Ave" ],
244+ : [ "city", "Anytown" ],
245+ : [ "state", "CA" ],
246+ : [ "postalCode", "98765-4321" ]
247+ : ]
248+ : ]
249+ : </pre>
250+ : would be represented as:
251+ : <pre>
252+ : &lt;person created="2006-11-11T19:23" modified="2006-12-31T23:59"&gt;
253+ : &lt;firstName&gt;Robert&lt;/firstName&gt;
254+ : &lt;lastName&gt;Smith&lt;/lastName&gt;
255+ : &lt;address type="home"&gt;
256+ : &lt;street&gt;12345 Sixth Ave&lt;/street&gt;
257+ : &lt;city&gt;Anytown&lt;/city&gt;
258+ : &lt;state&gt;CA&lt;/state&gt;
259+ : &lt;postalCode&gt;98765-4321&lt;/postalCode&gt;
260+ : &lt;/address&gt;
261+ : &lt;/person&gt;
262+ : </pre>
263+ :
264+ : @author Paul J. Lucas
265+ : @project data processing/data converters
266+ :)
267+module namespace json = "http://www.zorba-xquery.com/modules/converters/json";
268+
269+import module namespace schema = "http://www.zorba-xquery.com/modules/schema";
270+
271+import schema namespace json-options =
272+ "http://www.zorba-xquery.com/modules/converters/json-options";
273+
274+declare namespace ann = "http://www.zorba-xquery.com/annotations";
275+declare namespace err = "http://www.w3.org/2005/xqt-errors";
276+declare namespace zerr = "http://www.zorba-xquery.com/errors";
277+
278+declare namespace ver = "http://www.zorba-xquery.com/options/versioning";
279+declare option ver:module-version "2.0";
280+
281+(:~
282+ : Parses JSON data from a string and returns an XDM instance using one of the
283+ : representations described above.
284+ :
285+ : @param $json The JSON data to parse.
286+ : @param $options The parsing options, for example:
287+ : <pre>
288+ : &lt;options xmlns="http://www.zorba-xquery.com/modules/converters/json-options"&gt;
289+ : &lt;json-format value="JsonML-array"/&gt;
290+ : &lt;/options&gt;
291+ : </pre>
292+ : @return said XDM instance.
293+ : @error err:XQDY0027 if $options can not be validated against the
294+ : json-options schema.
295+ : @error zerr:ZJPE0001 if $json contains an illegal JSON character.
296+ : @error zerr:ZJPE0002 if $json contains an illegal Unicode code-point.
297+ : @error zerr:ZJPE0003 if $json contains an illegal JSON character escape.
298+ : @error zerr:ZJPE0004 if $json contains an illegal JSON literal.
299+ : @error zerr:ZJPE0005 if $json contains an illegal JSON number.
300+ : @error zerr:ZJPE0006 if $json is not a valid JSON string.
301+ : @error zerr:ZJPE0007 if $json contains an unterminated string.
302+ : @error zerr:ZJPE0008 if $json contains an illegal QName.
303+ : @example test/rbkt/Queries/zorba/json/json-jsonml_array-parse-01.xq
304+ :)
305+declare function json:parse(
306+ $json as xs:string?,
307+ $options as element(json-options:options)
308+) as element(*,xs:untyped)*
309+{
310+ let $validated-options := if ( schema:is-validated( $options ) ) then
311+ $options
312+ else
313+ validate { $options }
314+ return json:parse-internal( $json, $validated-options )
315+};
316+
317+(:~
318+ : Parses JSON data from a string and returns an XDM instance using the Snelson
319+ : representation described above.
320+ :
321+ : @param $json The JSON data to parse.
322+ : @return said XDM instance.
323+ : @error zerr:ZJPE0001 if $json contains an illegal JSON character.
324+ : @error zerr:ZJPE0002 if $json contains an illegal Unicode code-point.
325+ : @error zerr:ZJPE0003 if $json contains an illegal JSON character escape.
326+ : @error zerr:ZJPE0004 if $json contains an illegal JSON literal.
327+ : @error zerr:ZJPE0005 if $json contains an illegal JSON number.
328+ : @error zerr:ZJPE0006 if $json is not a valid JSON string.
329+ : @error zerr:ZJPE0007 if $json contains an unterminated string.
330+ : @error zerr:ZJPE0008 if $json contains an illegal QName.
331+ : @example test/rbkt/Queries/zorba/json/json-snelson-parse-array-01.xq
332+ :)
333+declare function json:parse(
334+ $json as xs:string?
335+) as element(*,xs:untyped)*
336+{
337+ json:parse-internal(
338+ $json,
339+ validate {
340+ <options
341+ xmlns="http://www.zorba-xquery.com/modules/converters/json-options">
342+ <json-format value="Snelson"/>
343+ </options>
344+ }
345+ )
346+};
347+
348+(:~
349+ : Serializes an XDM into JSON using one of the representations described
350+ : above.
351+ :
352+ : @param $xml The XDM to serialize.
353+ : @param $options The serializing options, for example:
354+ : <pre>
355+ : &lt;options xmlns="http://www.zorba-xquery.com/modules/converters/json-options"&gt;
356+ : &lt;json-format value="JsonML-array"/&gt;
357+ : &lt;whitespace value="indent"/&gt;
358+ : &lt;/options&gt;
359+ : </pre>
360+ : @return a JSON string.
361+ : @error err:XQDY0027 if $options can not be validated against the
362+ : json-options schema.
363+ : @error zerr:ZJSE0001 if $xml is not a document or element node.
364+ : @error zerr:ZJSE0002 if $xml contains an element that is missing a required
365+ : attribute.
366+ : @error zerr:ZJSE0003 if $xml contains an attribute having an illegal value.
367+ : @error zerr:ZJSE0004 if $xml contains an illegal element.
368+ : @error zerr:ZJSE0005 if $xml contains an illegal child element for a JSON
369+ : type.
370+ : @error zerr:ZJSE0006 if $xml contains an illegal child element.
371+ : @error zerr:ZJSE0007 if $xml contains an illegal text node.
372+ : @error zerr:ZJSE0008 if $xml contains an illegal value for a JSON type.
373+ : @example test/rbkt/Queries/zorba/json/json-jsonml_array-serialize-01.xq
374+ :)
375+declare function json:serialize(
376+ $xml as item()*,
377+ $options as element(json-options:options)
378+) as xs:string
379+{
380+ let $validated-options := if ( schema:is-validated( $options ) ) then
381+ $options
382+ else
383+ validate { $options }
384+ return json:serialize-internal( $xml, $validated-options )
385+};
386+
387+(:~
388+ : Serializes an XDM into JSON using one of the representations described
389+ : above.
390+ :
391+ : @param $xml The XDM to serialize.
392+ : @return a JSON string.
393+ : @error zerr:ZJSE0001 if $xml is not a document or element node.
394+ : @error zerr:ZJSE0002 if $xml contains an element that is missing a required
395+ : attribute.
396+ : @error zerr:ZJSE0003 if $xml contains an attribute having an illegal value.
397+ : @error zerr:ZJSE0004 if $xml contains an illegal element.
398+ : @error zerr:ZJSE0005 if $xml contains an illegal child element for a JSON
399+ : type.
400+ : @error zerr:ZJSE0006 if $xml contains an illegal child element.
401+ : @error zerr:ZJSE0007 if $xml contains an illegal text node.
402+ : @error zerr:ZJSE0008 if $xml contains an illegal value for a JSON type.
403+ : @example test/rbkt/Queries/zorba/json/json-snelson-serialize-array-01.xq
404+ :)
405+declare function json:serialize(
406+ $xml as item()*
407+) as xs:string
408+{
409+ json:serialize-internal($xml,
410+ validate {
411+ <options
412+ xmlns="http://www.zorba-xquery.com/modules/converters/json-options">
413+ <json-format value="Snelson"/>
414+ </options>
415+ }
416+ )
417+};
418+
419+(:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::)
420+
421+declare %private function json:parse-internal(
422+ $json as xs:string?,
423+ $options as item()?
424+) as element()* external;
425+
426+declare %ann:streamable %private function json:serialize-internal(
427+ $xml as item()*,
428+ $options as item()?
429+) as xs:string external;
430+
431+(: vim:set et sw=2 ts=2: :)
432
433=== modified file 'modules/com/zorba-xquery/www/modules/pregenerated/errors.xq'
434--- modules/com/zorba-xquery/www/modules/pregenerated/errors.xq 2011-12-21 14:40:33 +0000
435+++ modules/com/zorba-xquery/www/modules/pregenerated/errors.xq 2012-02-14 03:46:19 +0000
436@@ -784,4 +784,72 @@
437
438 (:~
439 :)
440-declare variable $zerr:XSST0010 as xs:QName := fn:QName($zerr:NS, "zerr:XSST0010");
441\ No newline at end of file
442+declare variable $zerr:XSST0010 as xs:QName := fn:QName($zerr:NS, "zerr:XSST0010");
443+
444+(:~
445+:)
446+declare variable $zerr:ZJPE0001 as xs:QName := fn:QName($zerr:NS, "zerr:ZJPE0001");
447+
448+(:~
449+:)
450+declare variable $zerr:ZJPE0002 as xs:QName := fn:QName($zerr:NS, "zerr:ZJPE0002");
451+
452+(:~
453+:)
454+declare variable $zerr:ZJPE0003 as xs:QName := fn:QName($zerr:NS, "zerr:ZJPE0003");
455+
456+(:~
457+:)
458+declare variable $zerr:ZJPE0004 as xs:QName := fn:QName($zerr:NS, "zerr:ZJPE0004");
459+
460+(:~
461+:)
462+declare variable $zerr:ZJPE0005 as xs:QName := fn:QName($zerr:NS, "zerr:ZJPE0005");
463+
464+(:~
465+:)
466+declare variable $zerr:ZJPE0006 as xs:QName := fn:QName($zerr:NS, "zerr:ZJPE0006");
467+
468+(:~
469+:)
470+declare variable $zerr:ZJPE0007 as xs:QName := fn:QName($zerr:NS, "zerr:ZJPE0007");
471+
472+(:~
473+:)
474+declare variable $zerr:ZJPE0008 as xs:QName := fn:QName($zerr:NS, "zerr:ZJPE0008");
475+
476+(:~
477+:)
478+declare variable $zerr:ZJPE0009 as xs:QName := fn:QName($zerr:NS, "zerr:ZJPE0009");
479+
480+(:~
481+:)
482+declare variable $zerr:ZJSE0001 as xs:QName := fn:QName($zerr:NS, "zerr:ZJSE0001");
483+
484+(:~
485+:)
486+declare variable $zerr:ZJSE0002 as xs:QName := fn:QName($zerr:NS, "zerr:ZJSE0002");
487+
488+(:~
489+:)
490+declare variable $zerr:ZJSE0003 as xs:QName := fn:QName($zerr:NS, "zerr:ZJSE0003");
491+
492+(:~
493+:)
494+declare variable $zerr:ZJSE0004 as xs:QName := fn:QName($zerr:NS, "zerr:ZJSE0004");
495+
496+(:~
497+:)
498+declare variable $zerr:ZJSE0005 as xs:QName := fn:QName($zerr:NS, "zerr:ZJSE0005");
499+
500+(:~
501+:)
502+declare variable $zerr:ZJSE0006 as xs:QName := fn:QName($zerr:NS, "zerr:ZJSE0006");
503+
504+(:~
505+:)
506+declare variable $zerr:ZJSE0007 as xs:QName := fn:QName($zerr:NS, "zerr:ZJSE0007");
507+
508+(:~
509+:)
510+declare variable $zerr:ZJSE0008 as xs:QName := fn:QName($zerr:NS, "zerr:ZJSE0008");
511\ No newline at end of file
512
513=== modified file 'src/context/static_context.cpp'
514--- src/context/static_context.cpp 2012-02-05 12:31:20 +0000
515+++ src/context/static_context.cpp 2012-02-14 03:46:19 +0000
516@@ -280,6 +280,11 @@
517 static_context::ZORBA_BASE64_FN_NS =
518 "http://www.zorba-xquery.com/modules/converters/base64";
519
520+
521+const char*
522+static_context::ZORBA_JSON_FN_NS =
523+"http://www.zorba-xquery.com/modules/converters/json";
524+
525 const char*
526 static_context::ZORBA_NODEREF_FN_NS =
527 "http://www.zorba-xquery.com/modules/node-reference";
528@@ -436,6 +441,7 @@
529 ns == ZORBA_REFLECTION_FN_NS ||
530 ns == ZORBA_SCRIPTING_FN_NS ||
531 ns == ZORBA_STRING_FN_NS ||
532+ ns == ZORBA_JSON_FN_NS ||
533 ns == ZORBA_FETCH_FN_NS ||
534 ns == ZORBA_NODE_FN_NS ||
535 ns == ZORBA_XML_FN_NS);
536@@ -482,6 +488,7 @@
537 {
538 return (ns == ZORBA_MATH_FN_NS ||
539 ns == ZORBA_INTROSP_SCTX_FN_NS ||
540+ ns == ZORBA_JSON_FN_NS ||
541 ns == ZORBA_RANDOM_FN_NS);
542 }
543
544
545=== modified file 'src/context/static_context.h'
546--- src/context/static_context.h 2012-02-02 09:56:52 +0000
547+++ src/context/static_context.h 2012-02-14 03:46:19 +0000
548@@ -446,6 +446,7 @@
549 // Namespaces of external modules declaring zorba builtin functions
550 static const char* ZORBA_MATH_FN_NS;
551 static const char* ZORBA_BASE64_FN_NS;
552+ static const char* ZORBA_JSON_FN_NS;
553 static const char* ZORBA_NODEREF_FN_NS;
554 static const char* ZORBA_NODEPOS_FN_NS;
555 static const char* ZORBA_STORE_DYNAMIC_COLLECTIONS_DDL_FN_NS;
556
557=== modified file 'src/diagnostics/diagnostic.cpp'
558--- src/diagnostics/diagnostic.cpp 2011-07-11 21:01:20 +0000
559+++ src/diagnostics/diagnostic.cpp 2012-02-14 03:46:19 +0000
560@@ -134,6 +134,9 @@
561 case ZORBA_STORE : o << "Zorba store" ; break;
562 case ZORBA_XQP : o << "Zorba" ; break;
563
564+ case JSON_PARSER : o << "JSON parser" ; break;
565+ case JSON_SERIALIZATION : o << "JSON serialization" ; break;
566+
567 default : /* suppresses warning */ break;
568 }
569 return o;
570
571=== modified file 'src/diagnostics/diagnostic_en.xml'
572--- src/diagnostics/diagnostic_en.xml 2011-12-21 14:40:33 +0000
573+++ src/diagnostics/diagnostic_en.xml 2012-02-14 03:46:19 +0000
574@@ -2307,6 +2307,63 @@
575 <value>"continue loop" statement not inside while statement</value>
576 </diagnostic>
577
578+ <!--////////// JSON Parse Errors ////////////////////////////////////////-->
579+
580+ <diagnostic code="ZJPE0001" name="ILLEGAL_CHARACTER">
581+ <value>'$1': illegal JSON character</value>
582+ </diagnostic>
583+ <diagnostic code="ZJPE0002" name="ILLEGAL_CODEPOINT">
584+ <value>"$1": illegal Unicode code-point</value>
585+ </diagnostic>
586+ <diagnostic code="ZJPE0003" name="ILLEGAL_ESCAPE">
587+ <value>'\\$1': illegal JSON character escape</value>
588+ </diagnostic>
589+ <diagnostic code="ZJPE0004" name="ILLEGAL_LITERAL">
590+ <value>illegal JSON literal</value>
591+ </diagnostic>
592+ <diagnostic code="ZJPE0005" name="ILLEGAL_NUMBER">
593+ <value>illegal JSON number</value>
594+ </diagnostic>
595+ <diagnostic code="ZJPE0006" name="UNEXPECTED_TOKEN">
596+ <value>"$1": unexpected JSON token</value>
597+ </diagnostic>
598+ <diagnostic code="ZJPE0007" name="UNTERMINATED_STRING">
599+ <value>unterminated JSON string</value>
600+ </diagnostic>
601+ <diagnostic code="ZJPE0008" name="ILLEGAL_QNAME">
602+ <value>"$1": illegal QName</value>
603+ </diagnostic>
604+ <diagnostic code="ZJPE0009" name="ILLEGAL_EMPTY_STRING">
605+ <value>illegal empty string</value>
606+ </diagnostic>
607+
608+ <!--////////// JSON Serialization Errors ////////////////////////////////-->
609+
610+ <diagnostic code="ZJSE0001" name="NOT_DOCUMENT_OR_ELEMENT_NODE">
611+ <value>JSON serialization requires document or element node</value>
612+ </diagnostic>
613+ <diagnostic code="ZJSE0002" name="ELEMENT_MISSING_ATTRIBUTE">
614+ <value>"$1" element missing required "$2" attribute</value>
615+ </diagnostic>
616+ <diagnostic code="ZJSE0003" name="BAD_ATTRIBUTE_VALUE">
617+ <value>"$1": illegal value for attribute "$2"</value>
618+ </diagnostic>
619+ <diagnostic code="ZJSE0004" name="BAD_ELEMENT">
620+ <value>"$1": illegal element${; must be "2"}${ or "3"}</value>
621+ </diagnostic>
622+ <diagnostic code="ZJSE0005" name="BAD_CHILD_ELEMENT">
623+ <value>"$1": illegal child element of "$2" type; must be "$3"</value>
624+ </diagnostic>
625+ <diagnostic code="ZJSE0006" name="NO_ELEMENT_CHILD">
626+ <value>JSON type "$1" can not have a child element node</value>
627+ </diagnostic>
628+ <diagnostic code="ZJSE0007" name="NO_TEXT_CHILD">
629+ <value>JSON type "$1" can not have a child text node</value>
630+ </diagnostic>
631+ <diagnostic code="ZJSE0008" name="BAD_VALUE">
632+ <value>"$1": illegal value for JSON type "$2"</value>
633+ </diagnostic>
634+
635 </namespace>
636
637 <!--////////// Zorba Warnings ////////////////////////////////////////////-->
638@@ -3264,6 +3321,14 @@
639 <value>Zorba warning</value>
640 </entry>
641
642+ <entry key="JSON parser error">
643+ <value>JSON parser error</value>
644+ </entry>
645+
646+ <entry key="JSON serialization error">
647+ <value>JSON serialization error</value>
648+ </entry>
649+
650 <entry key="dynamic error">
651 <value>dynamic error</value>
652 </entry>
653
654=== modified file 'src/diagnostics/pregenerated/diagnostic_list.cpp'
655--- src/diagnostics/pregenerated/diagnostic_list.cpp 2011-12-21 14:40:33 +0000
656+++ src/diagnostics/pregenerated/diagnostic_list.cpp 2012-02-14 03:46:19 +0000
657@@ -1097,6 +1097,57 @@
658 ZorbaErrorCode XSST0010( "XSST0010" );
659
660
661+ZorbaErrorCode ZJPE0001_ILLEGAL_CHARACTER( "ZJPE0001" );
662+
663+
664+ZorbaErrorCode ZJPE0002_ILLEGAL_CODEPOINT( "ZJPE0002" );
665+
666+
667+ZorbaErrorCode ZJPE0003_ILLEGAL_ESCAPE( "ZJPE0003" );
668+
669+
670+ZorbaErrorCode ZJPE0004_ILLEGAL_LITERAL( "ZJPE0004" );
671+
672+
673+ZorbaErrorCode ZJPE0005_ILLEGAL_NUMBER( "ZJPE0005" );
674+
675+
676+ZorbaErrorCode ZJPE0006_UNEXPECTED_TOKEN( "ZJPE0006" );
677+
678+
679+ZorbaErrorCode ZJPE0007_UNTERMINATED_STRING( "ZJPE0007" );
680+
681+
682+ZorbaErrorCode ZJPE0008_ILLEGAL_QNAME( "ZJPE0008" );
683+
684+
685+ZorbaErrorCode ZJPE0009_ILLEGAL_EMPTY_STRING( "ZJPE0009" );
686+
687+
688+ZorbaErrorCode ZJSE0001_NOT_DOCUMENT_OR_ELEMENT_NODE( "ZJSE0001" );
689+
690+
691+ZorbaErrorCode ZJSE0002_ELEMENT_MISSING_ATTRIBUTE( "ZJSE0002" );
692+
693+
694+ZorbaErrorCode ZJSE0003_BAD_ATTRIBUTE_VALUE( "ZJSE0003" );
695+
696+
697+ZorbaErrorCode ZJSE0004_BAD_ELEMENT( "ZJSE0004" );
698+
699+
700+ZorbaErrorCode ZJSE0005_BAD_CHILD_ELEMENT( "ZJSE0005" );
701+
702+
703+ZorbaErrorCode ZJSE0006_NO_ELEMENT_CHILD( "ZJSE0006" );
704+
705+
706+ZorbaErrorCode ZJSE0007_NO_TEXT_CHILD( "ZJSE0007" );
707+
708+
709+ZorbaErrorCode ZJSE0008_BAD_VALUE( "ZJSE0008" );
710+
711+
712 } // namespace zerr
713
714 namespace zwarn {
715
716=== modified file 'src/diagnostics/pregenerated/dict_en.cpp'
717--- src/diagnostics/pregenerated/dict_en.cpp 2011-12-21 14:40:33 +0000
718+++ src/diagnostics/pregenerated/dict_en.cpp 2012-02-14 03:46:19 +0000
719@@ -332,6 +332,23 @@
720 #if defined(ZORBA_WITH_DEBUGGER)
721 { "ZGDB0001", "" },
722 #endif
723+ { "ZJPE0001", "'$1': illegal JSON character" },
724+ { "ZJPE0002", "\"$1\": illegal Unicode code-point" },
725+ { "ZJPE0003", "'\\$1': illegal JSON character escape" },
726+ { "ZJPE0004", "illegal JSON literal" },
727+ { "ZJPE0005", "illegal JSON number" },
728+ { "ZJPE0006", "\"$1\": unexpected JSON token" },
729+ { "ZJPE0007", "unterminated JSON string" },
730+ { "ZJPE0008", "\"$1\": illegal QName" },
731+ { "ZJPE0009", "illegal empty string" },
732+ { "ZJSE0001", "JSON serialization requires document or element node" },
733+ { "ZJSE0002", "\"$1\" element missing required \"$2\" attribute" },
734+ { "ZJSE0003", "\"$1\": illegal value for attribute \"$2\"" },
735+ { "ZJSE0004", "\"$1\": illegal element${; must be \"2\"}${ or \"3\"}" },
736+ { "ZJSE0005", "\"$1\": illegal child element of \"$2\" type; must be \"$3\"" },
737+ { "ZJSE0006", "JSON type \"$1\" can not have a child element node" },
738+ { "ZJSE0007", "JSON type \"$1\" can not have a child text node" },
739+ { "ZJSE0008", "\"$1\": illegal value for JSON type \"$2\"" },
740 { "ZOSE0001", "\"$1\": file not found" },
741 { "ZOSE0002", "\"$1\": not plain file" },
742 { "ZOSE0003", "stream read failure" },
743@@ -494,6 +511,8 @@
744 { "~HexBinaryMustBeEven", "HexBinary value must contain an even number of characters" },
745 { "~IncompleteKeyInIndexBuild", "incomplete key during index build" },
746 { "~IncompleteKeyInIndexRefresh", "incomplete key during index refresh" },
747+ { "~JSON parser error", "JSON parser error" },
748+ { "~JSON serialization error", "JSON serialization error" },
749 { "~LibModVersionMismatch_3", "XQuery library version can not be imported by a $3 version module" },
750 { "~ModuleDeclNotInMain", "module declaration must not be in main module" },
751 { "~ModuleNotFound", "module not found" },
752
753=== modified file 'src/diagnostics/qname.cpp'
754--- src/diagnostics/qname.cpp 2011-07-01 16:07:54 +0000
755+++ src/diagnostics/qname.cpp 2012-02-14 03:46:19 +0000
756@@ -79,9 +79,15 @@
757 case 'C': return ZORBA_SERIALIZATION;
758 case 'D': return ZORBA_DDF;
759 case 'G': return ZORBA_DEBUGGER;
760+ case 'J': switch ( name[2] ) {
761+ case 'P': return JSON_PARSER;
762+ case 'S': return JSON_SERIALIZATION;
763+ default : ZORBA_ASSERT( false );
764+ }
765 case 'O': return ZORBA_OS;
766 case 'S': return ZORBA_STORE;
767 case 'X': return ZORBA_XQP;
768+
769 default : ZORBA_ASSERT( false );
770 }
771 }
772
773=== modified file 'src/functions/library.cpp'
774--- src/functions/library.cpp 2011-10-14 07:35:51 +0000
775+++ src/functions/library.cpp 2012-02-14 03:46:19 +0000
776@@ -57,6 +57,7 @@
777 #include "functions/func_sequences.h"
778 #include "functions/func_sequences_impl.h"
779 #include "functions/func_strings.h"
780+#include "functions/func_json.h"
781 #include "functions/func_var_decl.h"
782 #include "functions/func_xqdoc.h"
783 #include "functions/func_documents.h"
784@@ -107,6 +108,7 @@
785 populate_context_fnput(sctx);
786 populate_context_index_ddl(sctx);
787 populate_context_ic_ddl(sctx);
788+ populate_context_json(sctx);
789 populate_context_maths(sctx);
790 populate_context_nodes(sctx);
791 populate_context_node_position(sctx);
792
793=== added file 'src/functions/pregenerated/func_json.cpp'
794--- src/functions/pregenerated/func_json.cpp 1970-01-01 00:00:00 +0000
795+++ src/functions/pregenerated/func_json.cpp 2012-02-14 03:46:19 +0000
796@@ -0,0 +1,87 @@
797+/*
798+ * Copyright 2006-2008 The FLWOR Foundation.
799+ *
800+ * Licensed under the Apache License, Version 2.0 (the "License");
801+ * you may not use this file except in compliance with the License.
802+ * You may obtain a copy of the License at
803+ *
804+ * http://www.apache.org/licenses/LICENSE-2.0
805+ *
806+ * Unless required by applicable law or agreed to in writing, software
807+ * distributed under the License is distributed on an "AS IS" BASIS,
808+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
809+ * See the License for the specific language governing permissions and
810+ * limitations under the License.
811+ */
812+
813+// ******************************************
814+// * *
815+// * THIS IS A GENERATED FILE. DO NOT EDIT! *
816+// * SEE .xml FILE WITH SAME NAME *
817+// * *
818+// ******************************************
819+
820+
821+#include "stdafx.h"
822+#include "runtime/json/json.h"
823+#include "functions/func_json.h"
824+
825+
826+namespace zorba{
827+
828+
829+
830+PlanIter_t fn_zorba_json_parse_internal::codegen(
831+ CompilerCB*,
832+ static_context* sctx,
833+ const QueryLoc& loc,
834+ std::vector<PlanIter_t>& argv,
835+ AnnotationHolder& ann) const
836+{
837+ return new JSONParseInternal(sctx, loc, argv);
838+}
839+
840+PlanIter_t fn_zorba_json_serialize_internal::codegen(
841+ CompilerCB*,
842+ static_context* sctx,
843+ const QueryLoc& loc,
844+ std::vector<PlanIter_t>& argv,
845+ AnnotationHolder& ann) const
846+{
847+ return new JSONSerializeInternal(sctx, loc, argv);
848+}
849+
850+void populate_context_json(static_context* sctx)
851+{
852+ {
853+
854+
855+ DECL_WITH_KIND(sctx, fn_zorba_json_parse_internal,
856+ (createQName("http://www.zorba-xquery.com/modules/converters/json","","parse-internal"),
857+ GENV_TYPESYSTEM.STRING_TYPE_ONE,
858+ GENV_TYPESYSTEM.ITEM_TYPE_QUESTION,
859+ GENV_TYPESYSTEM.ELEMENT_TYPE_STAR),
860+ FunctionConsts::FN_ZORBA_JSON_PARSE_INTERNAL_2);
861+
862+ }
863+
864+
865+ {
866+
867+
868+ DECL_WITH_KIND(sctx, fn_zorba_json_serialize_internal,
869+ (createQName("http://www.zorba-xquery.com/modules/converters/json","","serialize-internal"),
870+ GENV_TYPESYSTEM.ITEM_TYPE_STAR,
871+ GENV_TYPESYSTEM.ITEM_TYPE_QUESTION,
872+ GENV_TYPESYSTEM.STRING_TYPE_ONE),
873+ FunctionConsts::FN_ZORBA_JSON_SERIALIZE_INTERNAL_2);
874+
875+ }
876+
877+}
878+
879+
880+}
881+
882+
883+
884
885=== added file 'src/functions/pregenerated/func_json.h'
886--- src/functions/pregenerated/func_json.h 1970-01-01 00:00:00 +0000
887+++ src/functions/pregenerated/func_json.h 2012-02-14 03:46:19 +0000
888@@ -0,0 +1,79 @@
889+/*
890+ * Copyright 2006-2008 The FLWOR Foundation.
891+ *
892+ * Licensed under the Apache License, Version 2.0 (the "License");
893+ * you may not use this file except in compliance with the License.
894+ * You may obtain a copy of the License at
895+ *
896+ * http://www.apache.org/licenses/LICENSE-2.0
897+ *
898+ * Unless required by applicable law or agreed to in writing, software
899+ * distributed under the License is distributed on an "AS IS" BASIS,
900+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
901+ * See the License for the specific language governing permissions and
902+ * limitations under the License.
903+ */
904+
905+// ******************************************
906+// * *
907+// * THIS IS A GENERATED FILE. DO NOT EDIT! *
908+// * SEE .xml FILE WITH SAME NAME *
909+// * *
910+// ******************************************
911+
912+
913+#ifndef ZORBA_FUNCTIONS_JSON_H
914+#define ZORBA_FUNCTIONS_JSON_H
915+
916+
917+#include "common/shared_types.h"
918+#include "functions/function_impl.h"
919+
920+
921+namespace zorba {
922+
923+
924+void populate_context_json(static_context* sctx);
925+
926+
927+
928+
929+//fn-zorba-json:parse-internal
930+class fn_zorba_json_parse_internal : public function
931+{
932+public:
933+ fn_zorba_json_parse_internal(const signature& sig, FunctionConsts::FunctionKind kind)
934+ :
935+ function(sig, kind)
936+ {
937+
938+ }
939+
940+ CODEGEN_DECL();
941+};
942+
943+
944+//fn-zorba-json:serialize-internal
945+class fn_zorba_json_serialize_internal : public function
946+{
947+public:
948+ fn_zorba_json_serialize_internal(const signature& sig, FunctionConsts::FunctionKind kind)
949+ :
950+ function(sig, kind)
951+ {
952+
953+ }
954+
955+ CODEGEN_DECL();
956+};
957+
958+
959+} //namespace zorba
960+
961+
962+#endif
963+/*
964+ * Local variables:
965+ * mode: c++
966+ * End:
967+ */
968
969=== modified file 'src/functions/pregenerated/function_enum.h'
970--- src/functions/pregenerated/function_enum.h 2012-01-11 17:30:25 +0000
971+++ src/functions/pregenerated/function_enum.h 2012-02-14 03:46:19 +0000
972@@ -166,6 +166,8 @@
973 FN_ZORBA_INTROSPECT_SCTX_IN_SCOPE_ATTRIBUTE_GROUPS_0,
974 FN_ZORBA_INTROSPECT_SCTX_OPTION_1,
975 FN_ZORBA_INTROSPECT_SCTX_FUNCTION_ANNOTATIONS_2,
976+ FN_ZORBA_JSON_PARSE_INTERNAL_2,
977+ FN_ZORBA_JSON_SERIALIZE_INTERNAL_2,
978 MATH_SQRT_1,
979 MATH_EXP_1,
980 MATH_EXP10_1,
981
982=== modified file 'src/runtime/CMakeLists.txt'
983--- src/runtime/CMakeLists.txt 2011-06-01 13:16:28 +0000
984+++ src/runtime/CMakeLists.txt 2012-02-14 03:46:19 +0000
985@@ -115,6 +115,9 @@
986 durations_dates_times/DurationsDatesTimesImpl.cpp
987 indexing/doc_indexer.cpp
988 indexing/index_ddl.cpp
989+ json/common.cpp
990+ json/jsonml_array.cpp
991+ json/snelson.cpp
992 numerics/NumericsImpl.cpp
993 numerics/format_integer_impl.cpp
994 sequences/SequencesImpl.cpp
995
996=== modified file 'src/runtime/full_text/ft_match.cpp'
997--- src/runtime/full_text/ft_match.cpp 2011-06-14 17:26:33 +0000
998+++ src/runtime/full_text/ft_match.cpp 2012-02-14 03:46:19 +0000
999@@ -31,7 +31,7 @@
1000 return o << "0x" << hex << reinterpret_cast<unsigned long>( obj ) << dec;
1001 }
1002
1003-DEF_OMANIP1( print_addr, void const*, obj )
1004+DEF_OMANIP1( print_addr, void const* )
1005
1006 ostream& operator<<( ostream &o, ft_string_match const &sm ) {
1007 return o << "{SM: "
1008@@ -52,8 +52,7 @@
1009 return o;
1010 }
1011
1012-DEF_OMANIP2( print_string_matches, char const*, label,
1013- ft_string_matches const&, sms )
1014+DEF_OMANIP2( print_string_matches, char const*, ft_string_matches const& )
1015
1016 ostream& operator<<( ostream &o, ft_match const &m ) {
1017 return o << indent << "ft_match @ " << print_addr( &m ) << '\n'
1018
1019=== added directory 'src/runtime/json'
1020=== added file 'src/runtime/json/common.cpp'
1021--- src/runtime/json/common.cpp 1970-01-01 00:00:00 +0000
1022+++ src/runtime/json/common.cpp 2012-02-14 03:46:19 +0000
1023@@ -0,0 +1,62 @@
1024+/*
1025+ * Copyright 2006-2011 The FLWOR Foundation.
1026+ *
1027+ * Licensed under the Apache License, Version 2.0 (the "License");
1028+ * you may not use this file except in compliance with the License.
1029+ * You may obtain a copy of the License at
1030+ *
1031+ * http://www.apache.org/licenses/LICENSE-2.0
1032+ *
1033+ * Unless required by applicable law or agreed to in writing, software
1034+ * distributed under the License is distributed on an "AS IS" BASIS,
1035+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1036+ * See the License for the specific language governing permissions and
1037+ * limitations under the License.
1038+ */
1039+#include "stdafx.h"
1040+
1041+#include "store/api/iterator.h"
1042+
1043+#include "common.h"
1044+
1045+using namespace std;
1046+
1047+namespace zorba {
1048+
1049+///////////////////////////////////////////////////////////////////////////////
1050+
1051+bool get_attribute_value( store::Item_t const &element, char const *att_name,
1052+ zstring *att_value ) {
1053+ store::Iterator_t i( element->getAttributes() );
1054+ bool found = false;
1055+ i->open();
1056+ store::Item_t att_item;
1057+ while ( i->next( att_item ) ) {
1058+ if ( att_item->getNodeName()->getStringValue() == att_name ) {
1059+ att_item->getStringValue2( *att_value );
1060+ found = true;
1061+ break;
1062+ }
1063+ }
1064+ i->close();
1065+ return found;
1066+}
1067+
1068+///////////////////////////////////////////////////////////////////////////////
1069+
1070+#if ZORBA_DEBUG_JSON
1071+
1072+ostream& operator<<( ostream &o, parse_state s ) {
1073+ static char const *const string_of[] = {
1074+ "in_array",
1075+ "in_object"
1076+ };
1077+ return o << string_of[ s ];
1078+}
1079+
1080+#endif /* ZORBA_DEBUG_JSON */
1081+
1082+///////////////////////////////////////////////////////////////////////////////
1083+
1084+} // namespace zorba
1085+/* vim:set et sw=2 ts=2: */
1086
1087=== added file 'src/runtime/json/common.h'
1088--- src/runtime/json/common.h 1970-01-01 00:00:00 +0000
1089+++ src/runtime/json/common.h 2012-02-14 03:46:19 +0000
1090@@ -0,0 +1,120 @@
1091+/*
1092+ * Copyright 2006-2011 The FLWOR Foundation.
1093+ *
1094+ * Licensed under the Apache License, Version 2.0 (the "License");
1095+ * you may not use this file except in compliance with the License.
1096+ * You may obtain a copy of the License at
1097+ *
1098+ * http://www.apache.org/licenses/LICENSE-2.0
1099+ *
1100+ * Unless required by applicable law or agreed to in writing, software
1101+ * distributed under the License is distributed on an "AS IS" BASIS,
1102+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1103+ * See the License for the specific language governing permissions and
1104+ * limitations under the License.
1105+ */
1106+
1107+#ifndef ZORBA_RUNTIME_JSON_COMMON_H
1108+#define ZORBA_RUNTIME_JSON_COMMON_H
1109+
1110+#include <iostream>
1111+#include <stack>
1112+
1113+#include "store/api/item.h"
1114+#include "store/api/item_factory.h"
1115+#include "util/indent.h"
1116+#include "util/omanip.h"
1117+#include "zorbatypes/zstring.h"
1118+
1119+#define ZORBA_DEBUG_JSON 0
1120+
1121+namespace zorba {
1122+
1123+///////////////////////////////////////////////////////////////////////////////
1124+
1125+typedef std::stack<store::Item*> item_stack_type;
1126+
1127+enum parse_state {
1128+ in_array,
1129+ in_object
1130+};
1131+
1132+typedef std::stack<int> state_stack_type;
1133+
1134+namespace whitespace {
1135+ enum type {
1136+ none,
1137+ some,
1138+ indent
1139+ };
1140+}
1141+
1142+///////////////////////////////////////////////////////////////////////////////
1143+
1144+bool get_attribute_value( store::Item_t const &element, char const *att_name,
1145+ zstring *att_value );
1146+
1147+typedef std::ostream& (*std_omanip_type)(std::ostream&);
1148+
1149+inline std::ostream& if_do( std::ostream &o, bool expr, std_omanip_type fn ) {
1150+ if ( expr )
1151+ o << fn;
1152+ return o;
1153+}
1154+DEF_OMANIP2( if_do, bool, std_omanip_type )
1155+
1156+#define if_indent(WS,FN) if_do( (WS) == whitespace::indent, FN )
1157+
1158+inline std::ostream& if_emit( std::ostream &o, bool expr, char c ) {
1159+ if ( expr )
1160+ o << c;
1161+ return o;
1162+}
1163+DEF_OMANIP2( if_emit, bool, char )
1164+
1165+///////////////////////////////////////////////////////////////////////////////
1166+
1167+#define IN_STATE(S) ztd::top_stack_equals( state_stack, (S) )
1168+
1169+#if ZORBA_DEBUG_JSON
1170+
1171+std::ostream& operator<<( std::ostream &o, parse_state s );
1172+
1173+# define PUSH_ITEM(I) \
1174+ do { \
1175+ cout << __LINE__ << ":PUSH_ITEM( " << (I)->show() << " )" << endl; \
1176+ item_stack.push( (I).getp() ); \
1177+ } while (0)
1178+
1179+# define POP_ITEM() \
1180+ do { \
1181+ cout << __LINE__ << ":POP_ITEM()" << endl; \
1182+ cur_item = ztd::pop_stack( item_stack ); \
1183+ } while (0)
1184+
1185+# define PUSH_STATE(S) \
1186+ do { \
1187+ cout << __LINE__ << ":PUSH_STATE( " << (S) << " )" << endl; \
1188+ state_stack.push( S ); \
1189+ } while (0)
1190+
1191+# define POP_STATE() \
1192+ do { \
1193+ cout << __LINE__ << ":POP_STATE()" << endl; \
1194+ state_stack.pop(); \
1195+ } while (0) \
1196+
1197+#else
1198+
1199+# define PUSH_ITEM(I) item_stack.push( (I).getp() )
1200+# define POP_ITEM() cur_item = ztd::pop_stack( item_stack )
1201+# define PUSH_STATE(S) state_stack.push( S )
1202+# define POP_STATE() state_stack.pop()
1203+
1204+#endif /* ZORBA_DEBUG_JSON */
1205+
1206+///////////////////////////////////////////////////////////////////////////////
1207+
1208+} // namespace zorba
1209+#endif /* ZORBA_RUNTIME_JSON_COMMON_H */
1210+/* vim:set et sw=2 ts=2: */
1211
1212=== added file 'src/runtime/json/json_impl.cpp'
1213--- src/runtime/json/json_impl.cpp 1970-01-01 00:00:00 +0000
1214+++ src/runtime/json/json_impl.cpp 2012-02-14 03:46:19 +0000
1215@@ -0,0 +1,217 @@
1216+/*
1217+ * Copyright 2006-2011 The FLWOR Foundation.
1218+ *
1219+ * Licensed under the Apache License, Version 2.0 (the "License");
1220+ * you may not use this file except in compliance with the License.
1221+ * You may obtain a copy of the License at
1222+ *
1223+ * http://www.apache.org/licenses/LICENSE-2.0
1224+ *
1225+ * Unless required by applicable law or agreed to in writing, software
1226+ * distributed under the License is distributed on an "AS IS" BASIS,
1227+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1228+ * See the License for the specific language governing permissions and
1229+ * limitations under the License.
1230+ */
1231+#include "stdafx.h"
1232+
1233+#include <map>
1234+#include <sstream>
1235+
1236+#include <zorba/diagnostic_list.h>
1237+
1238+#include "runtime/json/json.h"
1239+#include "store/api/item_factory.h"
1240+#include "system/globalenv.h"
1241+#include "util/mem_streambuf.h"
1242+
1243+#include "jsonml_array.h"
1244+#include "snelson.h"
1245+
1246+using namespace std;
1247+
1248+namespace zorba {
1249+
1250+///////////////////////////////////////////////////////////////////////////////
1251+
1252+typedef map<zstring,zstring> options_type;
1253+
1254+static void get_options( store::Item_t const &options_element,
1255+ options_type *options ) {
1256+ ZORBA_ASSERT( options_element->getNodeKind() ==
1257+ store::StoreConsts::elementNode );
1258+ store::Iterator_t i = options_element->getChildren();
1259+ i->open();
1260+ store::Item_t option_item;
1261+ while ( i->next( option_item ) ) {
1262+ if ( option_item->getNodeKind() == store::StoreConsts::elementNode ) {
1263+ zstring const name( option_item->getNodeName()->getStringValue() );
1264+ zstring value;
1265+ get_attribute_value( option_item, "value", &value );
1266+ (*options)[ name ] = value;
1267+ }
1268+ }
1269+ i->close();
1270+}
1271+
1272+///////////////////////////////////////////////////////////////////////////////
1273+
1274+bool JSONParseInternal::nextImpl( store::Item_t& result,
1275+ PlanState &planState ) const {
1276+ store::Item_t cur_item;
1277+ options_type options;
1278+ istringstream iss;
1279+ mem_streambuf buf;
1280+
1281+ PlanIteratorState *state;
1282+ DEFAULT_STACK_INIT( PlanIteratorState, state, planState );
1283+
1284+ ZORBA_ASSERT( theChildren.size() == 2 );
1285+ consumeNext( cur_item, theChildren[1], planState );
1286+ get_options( cur_item, &options );
1287+
1288+ consumeNext( cur_item, theChildren[0], planState );
1289+ result = nullptr;
1290+
1291+ istream *is;
1292+ if ( cur_item->isStreamable() ) {
1293+ is = &cur_item->getStream();
1294+ } else {
1295+ zstring s;
1296+ cur_item->getStringValue2( s );
1297+ // Doing it this way uses the string data in-place with no copy.
1298+ buf.set( s.data(), s.size() );
1299+ iss.ios::rdbuf( &buf );
1300+ is = &iss;
1301+ }
1302+
1303+ try {
1304+ json::parser p( *is );
1305+ p.set_loc(
1306+ loc.getFilename().c_str(), loc.getLineBegin(), loc.getColumnBegin()
1307+ );
1308+
1309+ options_type::mapped_type const &format = options[ "json-format" ];
1310+ ZORBA_ASSERT( !format.empty() );
1311+ if ( format == "Snelson" )
1312+ snelson::parse( p, &result );
1313+ else if ( format == "JsonML-array" )
1314+ jsonml_array::parse( p, &result );
1315+ else
1316+ ZORBA_ASSERT( false );
1317+ }
1318+ catch ( json::illegal_character const &e ) {
1319+ throw XQUERY_EXCEPTION(
1320+ zerr::ZJPE0001_ILLEGAL_CHARACTER,
1321+ ERROR_PARAMS( e.get_char() ),
1322+ ERROR_LOC( e.get_loc() )
1323+ );
1324+ }
1325+ catch ( json::illegal_codepoint const &e ) {
1326+ throw XQUERY_EXCEPTION(
1327+ zerr::ZJPE0002_ILLEGAL_CODEPOINT,
1328+ ERROR_PARAMS( e.get_codepoint() ),
1329+ ERROR_LOC( e.get_loc() )
1330+ );
1331+ }
1332+ catch ( json::illegal_escape const &e ) {
1333+ throw XQUERY_EXCEPTION(
1334+ zerr::ZJPE0003_ILLEGAL_ESCAPE,
1335+ ERROR_PARAMS( e.get_escape() ),
1336+ ERROR_LOC( e.get_loc() )
1337+ );
1338+ }
1339+ catch ( json::illegal_literal const &e ) {
1340+ throw XQUERY_EXCEPTION(
1341+ zerr::ZJPE0004_ILLEGAL_LITERAL,
1342+ ERROR_LOC( e.get_loc() )
1343+ );
1344+ }
1345+ catch ( json::illegal_number const &e ) {
1346+ throw XQUERY_EXCEPTION(
1347+ zerr::ZJPE0005_ILLEGAL_NUMBER,
1348+ ERROR_LOC( e.get_loc() )
1349+ );
1350+ }
1351+ catch ( json::unexpected_token const &e ) {
1352+ throw XQUERY_EXCEPTION(
1353+ zerr::ZJPE0006_UNEXPECTED_TOKEN,
1354+ ERROR_PARAMS( e.get_token() ),
1355+ ERROR_LOC( e.get_loc() )
1356+ );
1357+ }
1358+ catch ( json::unterminated_string const &e ) {
1359+ throw XQUERY_EXCEPTION(
1360+ zerr::ZJPE0007_UNTERMINATED_STRING,
1361+ ERROR_LOC( e.get_loc() )
1362+ );
1363+ }
1364+
1365+ STACK_PUSH( !!result, state );
1366+ STACK_END( state );
1367+}
1368+
1369+///////////////////////////////////////////////////////////////////////////////
1370+
1371+bool JSONSerializeInternal::nextImpl( store::Item_t& result,
1372+ PlanState &planState ) const {
1373+ store::Item_t cur_item;
1374+ options_type options;
1375+
1376+ PlanIteratorState *state;
1377+ DEFAULT_STACK_INIT( PlanIteratorState, state, planState );
1378+
1379+ ZORBA_ASSERT( theChildren.size() == 2 );
1380+ consumeNext( cur_item, theChildren[1], planState );
1381+ get_options( cur_item, &options );
1382+
1383+ consumeNext( cur_item, theChildren[0], planState );
1384+ try {
1385+ options_type::mapped_type const &format_opt = options[ "json-format" ];
1386+ ZORBA_ASSERT( !format_opt.empty() );
1387+
1388+ whitespace::type ws;
1389+ options_type::mapped_type const &whitespace_opt = options[ "whitespace" ];
1390+ if ( whitespace_opt.empty() || whitespace_opt == "none" )
1391+ ws = whitespace::none;
1392+ else if ( whitespace_opt == "some" )
1393+ ws = whitespace::some;
1394+ else if ( whitespace_opt == "indent" )
1395+ ws = whitespace::indent;
1396+ else
1397+ ZORBA_ASSERT( false );
1398+
1399+ ostringstream oss;
1400+ switch ( cur_item->getNodeKind() ) {
1401+ case store::StoreConsts::documentNode:
1402+ case store::StoreConsts::elementNode:
1403+ if ( format_opt == "Snelson" )
1404+ snelson::serialize( oss, cur_item, ws );
1405+ else if ( format_opt == "JsonML-array" )
1406+ jsonml_array::serialize( oss, cur_item, ws );
1407+ else
1408+ ZORBA_ASSERT( false );
1409+ break;
1410+ default:
1411+ throw XQUERY_EXCEPTION(
1412+ zerr::ZJSE0001_NOT_DOCUMENT_OR_ELEMENT_NODE,
1413+ ERROR_LOC( loc )
1414+ );
1415+ }
1416+ // This string copying is inefficient, but I can't see another way.
1417+ zstring temp( oss.str() );
1418+ GENV_ITEMFACTORY->createString( result, temp );
1419+ }
1420+ catch ( ZorbaException &e ) {
1421+ set_source( e, loc );
1422+ throw;
1423+ }
1424+
1425+ STACK_PUSH( !!result, state );
1426+ STACK_END( state );
1427+}
1428+
1429+///////////////////////////////////////////////////////////////////////////////
1430+
1431+} // namespace zorba
1432+/* vim:set et sw=2 ts=2: */
1433
1434=== added file 'src/runtime/json/jsonml_array.cpp'
1435--- src/runtime/json/jsonml_array.cpp 1970-01-01 00:00:00 +0000
1436+++ src/runtime/json/jsonml_array.cpp 2012-02-14 03:46:19 +0000
1437@@ -0,0 +1,285 @@
1438+/*
1439+ * Copyright 2006-2011 The FLWOR Foundation.
1440+ *
1441+ * Licensed under the Apache License, Version 2.0 (the "License");
1442+ * you may not use this file except in compliance with the License.
1443+ * You may obtain a copy of the License at
1444+ *
1445+ * http://www.apache.org/licenses/LICENSE-2.0
1446+ *
1447+ * Unless required by applicable law or agreed to in writing, software
1448+ * distributed under the License is distributed on an "AS IS" BASIS,
1449+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1450+ * See the License for the specific language governing permissions and
1451+ * limitations under the License.
1452+ */
1453+#include "stdafx.h"
1454+
1455+#include <sstream>
1456+
1457+#include <zorba/diagnostic_list.h>
1458+
1459+#include "runtime/json/json.h"
1460+#include "store/api/item_factory.h"
1461+#include "system/globalenv.h"
1462+#include "types/root_typemanager.h"
1463+#include "util/ascii_util.h"
1464+#include "util/cxx_util.h"
1465+#include "util/json_parser.h"
1466+#include "util/mem_streambuf.h"
1467+#include "util/omanip.h"
1468+#include "util/oseparator.h"
1469+#include "util/stl_util.h"
1470+
1471+#include "jsonml_array.h"
1472+
1473+using namespace std;
1474+
1475+namespace zorba {
1476+
1477+///////////////////////////////////////////////////////////////////////////////
1478+
1479+static void split_name( zstring const &name, zstring *prefix, zstring *local ) {
1480+ zstring::size_type const colon = name.find( ':' );
1481+ if ( colon != zstring::npos ) {
1482+ *prefix = name.substr( 0, colon );
1483+ *local = name.substr( colon + 1 );
1484+ if ( prefix->empty() || local->empty() )
1485+ throw XQUERY_EXCEPTION(
1486+ zerr::ZJPE0008_ILLEGAL_QNAME,
1487+ ERROR_PARAMS( name )
1488+ );
1489+ } else {
1490+ prefix->clear();
1491+ *local = name;
1492+ }
1493+}
1494+
1495+namespace expect {
1496+ enum type {
1497+ none,
1498+ element_name,
1499+ attribute_name,
1500+ attribute_value
1501+ };
1502+}
1503+
1504+///////////////////////////////////////////////////////////////////////////////
1505+
1506+namespace jsonml_array {
1507+
1508+void parse( json::parser &p, store::Item_t *result ) {
1509+ ZORBA_ASSERT( result );
1510+
1511+ state_stack_type state_stack;
1512+
1513+ store::Item_t cur_item, junk_item, value_item;
1514+ store::Item_t att_name, element_name, type_name;
1515+
1516+ zstring base_uri;
1517+ bool got_something = false;
1518+ item_stack_type item_stack;
1519+ expect::type expect_what = expect::none;
1520+ store::NsBindings ns_bindings;
1521+ zstring value;
1522+
1523+ json::token token;
1524+ while ( p.next( &token ) ) {
1525+ got_something = true;
1526+ switch ( token.get_type() ) {
1527+
1528+ case '[':
1529+ PUSH_STATE( in_array );
1530+ expect_what = expect::element_name;
1531+ break;
1532+
1533+ case '{':
1534+ PUSH_STATE( in_object );
1535+ expect_what = expect::attribute_name;
1536+ break;
1537+
1538+ case ']':
1539+ POP_ITEM();
1540+ // no break;
1541+ case '}':
1542+ POP_STATE();
1543+ expect_what = expect::none;
1544+ break;
1545+
1546+ case ',':
1547+ expect_what = IN_STATE( in_object ) ?
1548+ expect::attribute_name : expect::none;
1549+ break;
1550+
1551+ case ':':
1552+ expect_what = expect::attribute_value;
1553+ break;
1554+
1555+ case json::token::number:
1556+ case 'F':
1557+ case 'T':
1558+ case json::token::json_null:
1559+ case json::token::string: {
1560+ value = token.get_value();
1561+ zstring prefix, local;
1562+ switch ( expect_what ) {
1563+ case expect::element_name:
1564+ split_name( value, &prefix, &local );
1565+ GENV_ITEMFACTORY->createQName( element_name, "", prefix, local );
1566+ type_name = GENV_TYPESYSTEM.XS_UNTYPED_QNAME;
1567+ GENV_ITEMFACTORY->createElementNode(
1568+ cur_item,
1569+ item_stack.empty() ? nullptr : item_stack.top(),
1570+ element_name, type_name, false, false, ns_bindings, base_uri
1571+ );
1572+ PUSH_ITEM( cur_item );
1573+ if ( !*result )
1574+ *result = cur_item;
1575+ break;
1576+ case expect::attribute_name:
1577+ split_name( value, &prefix, &local );
1578+ GENV_ITEMFACTORY->createQName( att_name, "", prefix, local );
1579+ break;
1580+ case expect::attribute_value:
1581+ type_name = GENV_TYPESYSTEM.XS_UNTYPED_QNAME;
1582+ GENV_ITEMFACTORY->createString( value_item, value );
1583+ GENV_ITEMFACTORY->createAttributeNode(
1584+ junk_item, cur_item, att_name, type_name, value_item
1585+ );
1586+ break;
1587+ case expect::none:
1588+ GENV_ITEMFACTORY->createTextNode( junk_item, cur_item, value );
1589+ break;
1590+ }
1591+ break;
1592+ }
1593+
1594+ case json::token::none:
1595+ break;
1596+
1597+ default:
1598+ assert( false );
1599+ } // switch
1600+ } // while
1601+ if ( !got_something )
1602+ throw XQUERY_EXCEPTION( zerr::ZJPE0009_ILLEGAL_EMPTY_STRING );
1603+}
1604+
1605+} // namespace jsonml_array
1606+
1607+///////////////////////////////////////////////////////////////////////////////
1608+
1609+static ostream& serialize_attributes( ostream &o, store::Item_t const &element,
1610+ oseparator &sep, whitespace::type ws ) {
1611+ bool emitted_attributes = false;
1612+ oseparator att_sep;
1613+ switch ( ws ) {
1614+ case whitespace::none : att_sep.sep( "," ); break;
1615+ case whitespace::some : att_sep.sep( ", " ); break;
1616+ case whitespace::indent: att_sep.sep( ",\n" ); break;
1617+ }
1618+
1619+ store::Iterator_t i( element->getAttributes() );
1620+ i->open();
1621+ store::Item_t att_item;
1622+ while ( i->next( att_item ) ) {
1623+ zstring const att_name( att_item->getNodeName()->getStringValue() );
1624+ if ( att_name == "xmlns" )
1625+ continue;
1626+ if ( !emitted_attributes ) {
1627+ o << sep
1628+ << if_emit( ws == whitespace::indent, '\n' )
1629+ << if_indent( ws, indent ) << '{'
1630+ << if_indent( ws, inc_indent );
1631+ emitted_attributes = true;
1632+ }
1633+ bool const was_printing = att_sep.printing();
1634+ o << att_sep;
1635+ if ( was_printing )
1636+ o << if_indent( ws, indent );
1637+ else
1638+ o << if_emit( ws, ' ' );
1639+
1640+ o << '"' << att_name << '"'
1641+ << if_emit( ws, ' ' ) << ':' << if_emit( ws, ' ' )
1642+ << '"' << att_item->getStringValue() << '"';
1643+ }
1644+ i->close();
1645+ if ( emitted_attributes )
1646+ o << if_emit( ws, ' ' ) << '}' << if_indent( ws, dec_indent );
1647+ return o;
1648+}
1649+DEF_OMANIP3( serialize_attributes, store::Item_t const&, oseparator&,
1650+ whitespace::type )
1651+
1652+static ostream& serialize_children( ostream&, store::Item_t const &parent,
1653+ oseparator&, whitespace::type );
1654+DEF_OMANIP3( serialize_children, store::Item_t const&, oseparator&,
1655+ whitespace::type )
1656+
1657+static ostream& serialize_element( ostream &o, store::Item_t const &element,
1658+ oseparator &sep, whitespace::type ws ) {
1659+ if ( sep.printing() )
1660+ o << if_emit( ws == whitespace::indent, '\n' );
1661+ sep.printing( true );
1662+ o << if_indent( ws, indent ) << '[' << if_emit( ws, ' ' )
1663+ << '"' << element->getNodeName()->getStringValue() << '"'
1664+ << if_indent( ws, inc_indent )
1665+ << serialize_attributes( element, sep, ws )
1666+ << serialize_children( element, sep, ws )
1667+ << if_emit( ws, ' ' ) << ']'
1668+ << if_indent( ws, dec_indent );
1669+ return o;
1670+}
1671+DEF_OMANIP3( serialize_element, store::Item_t const&, oseparator&,
1672+ whitespace::type )
1673+
1674+static ostream& serialize_children( ostream &o, store::Item_t const &parent,
1675+ oseparator &sep, whitespace::type ws ) {
1676+ store::Iterator_t i( parent->getChildren() );
1677+ i->open();
1678+ store::Item_t child;
1679+ while ( i->next( child ) ) {
1680+ switch ( child->getNodeKind() ) {
1681+ case store::StoreConsts::elementNode:
1682+ o << sep << serialize_element( child, sep, ws );
1683+ break;
1684+ case store::StoreConsts::textNode:
1685+ o << sep << '"' << child->getStringValue() << '"';
1686+ break;
1687+ default:
1688+ break;
1689+ }
1690+ }
1691+ i->close();
1692+ return o;
1693+}
1694+
1695+///////////////////////////////////////////////////////////////////////////////
1696+
1697+namespace jsonml_array {
1698+
1699+void serialize( ostream &o, store::Item_t const &item, whitespace::type ws ) {
1700+ oseparator sep;
1701+ if ( ws )
1702+ sep.sep( ", " );
1703+ else
1704+ sep.sep( "," );
1705+ switch ( item->getNodeKind() ) {
1706+ case store::StoreConsts::documentNode:
1707+ o << serialize_children( item, sep, ws );
1708+ break;
1709+ case store::StoreConsts::elementNode:
1710+ o << serialize_element( item, sep, ws );
1711+ break;
1712+ default:
1713+ throw XQUERY_EXCEPTION( zerr::ZJSE0001_NOT_DOCUMENT_OR_ELEMENT_NODE );
1714+ }
1715+}
1716+
1717+} // namespace jsonml_array
1718+
1719+///////////////////////////////////////////////////////////////////////////////
1720+
1721+} // namespace zorba
1722+/* vim:set et sw=2 ts=2: */
1723
1724=== added file 'src/runtime/json/jsonml_array.h'
1725--- src/runtime/json/jsonml_array.h 1970-01-01 00:00:00 +0000
1726+++ src/runtime/json/jsonml_array.h 2012-02-14 03:46:19 +0000
1727@@ -0,0 +1,41 @@
1728+/*
1729+ * Copyright 2006-2011 The FLWOR Foundation.
1730+ *
1731+ * Licensed under the Apache License, Version 2.0 (the "License");
1732+ * you may not use this file except in compliance with the License.
1733+ * You may obtain a copy of the License at
1734+ *
1735+ * http://www.apache.org/licenses/LICENSE-2.0
1736+ *
1737+ * Unless required by applicable law or agreed to in writing, software
1738+ * distributed under the License is distributed on an "AS IS" BASIS,
1739+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1740+ * See the License for the specific language governing permissions and
1741+ * limitations under the License.
1742+ */
1743+#include "stdafx.h"
1744+
1745+#ifndef ZORBA_RUNTIME_JSON_JSONML_ARRAY_H
1746+#define ZORBA_RUNTIME_JSON_JSONML_ARRAY_H
1747+
1748+#include <iostream>
1749+
1750+#include "store/api/item.h"
1751+#include "util/json_parser.h"
1752+
1753+#include "common.h"
1754+
1755+namespace zorba {
1756+namespace jsonml_array {
1757+
1758+///////////////////////////////////////////////////////////////////////////////
1759+
1760+void parse( json::parser &p, store::Item_t *result );
1761+void serialize( std::ostream&, store::Item_t const &item, whitespace::type );
1762+
1763+///////////////////////////////////////////////////////////////////////////////
1764+
1765+} // namespace jsonml_array
1766+} // namespace zorba
1767+#endif /* ZORBA_RUNTIME_JSON_JSONML_ARRAY_H */
1768+/* vim:set et sw=2 ts=2: */
1769
1770=== added directory 'src/runtime/json/pregenerated'
1771=== added file 'src/runtime/json/pregenerated/json.cpp'
1772--- src/runtime/json/pregenerated/json.cpp 1970-01-01 00:00:00 +0000
1773+++ src/runtime/json/pregenerated/json.cpp 2012-02-14 03:46:19 +0000
1774@@ -0,0 +1,94 @@
1775+/*
1776+ * Copyright 2006-2008 The FLWOR Foundation.
1777+ *
1778+ * Licensed under the Apache License, Version 2.0 (the "License");
1779+ * you may not use this file except in compliance with the License.
1780+ * You may obtain a copy of the License at
1781+ *
1782+ * http://www.apache.org/licenses/LICENSE-2.0
1783+ *
1784+ * Unless required by applicable law or agreed to in writing, software
1785+ * distributed under the License is distributed on an "AS IS" BASIS,
1786+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1787+ * See the License for the specific language governing permissions and
1788+ * limitations under the License.
1789+ */
1790+
1791+// ******************************************
1792+// * *
1793+// * THIS IS A GENERATED FILE. DO NOT EDIT! *
1794+// * SEE .xml FILE WITH SAME NAME *
1795+// * *
1796+// ******************************************
1797+
1798+#include "stdafx.h"
1799+#include "zorbatypes/rchandle.h"
1800+#include "zorbatypes/zstring.h"
1801+#include "runtime/visitors/planiter_visitor.h"
1802+#include "runtime/json/json.h"
1803+#include "system/globalenv.h"
1804+
1805+
1806+
1807+namespace zorba {
1808+
1809+// <JSONParseInternal>
1810+const char* JSONParseInternal::class_name_str = "JSONParseInternal";
1811+JSONParseInternal::class_factory<JSONParseInternal>
1812+JSONParseInternal::g_class_factory;
1813+
1814+const serialization::ClassVersion
1815+JSONParseInternal::class_versions[] ={{ 1, 0x000905, false}};
1816+
1817+const int JSONParseInternal::class_versions_count =
1818+sizeof(JSONParseInternal::class_versions)/sizeof(struct serialization::ClassVersion);
1819+
1820+void JSONParseInternal::accept(PlanIterVisitor& v) const {
1821+ v.beginVisit(*this);
1822+
1823+ std::vector<PlanIter_t>::const_iterator lIter = theChildren.begin();
1824+ std::vector<PlanIter_t>::const_iterator lEnd = theChildren.end();
1825+ for ( ; lIter != lEnd; ++lIter ){
1826+ (*lIter)->accept(v);
1827+ }
1828+
1829+ v.endVisit(*this);
1830+}
1831+
1832+JSONParseInternal::~JSONParseInternal() {}
1833+
1834+// </JSONParseInternal>
1835+
1836+
1837+// <JSONSerializeInternal>
1838+const char* JSONSerializeInternal::class_name_str = "JSONSerializeInternal";
1839+JSONSerializeInternal::class_factory<JSONSerializeInternal>
1840+JSONSerializeInternal::g_class_factory;
1841+
1842+const serialization::ClassVersion
1843+JSONSerializeInternal::class_versions[] ={{ 1, 0x000905, false}};
1844+
1845+const int JSONSerializeInternal::class_versions_count =
1846+sizeof(JSONSerializeInternal::class_versions)/sizeof(struct serialization::ClassVersion);
1847+
1848+void JSONSerializeInternal::accept(PlanIterVisitor& v) const {
1849+ v.beginVisit(*this);
1850+
1851+ std::vector<PlanIter_t>::const_iterator lIter = theChildren.begin();
1852+ std::vector<PlanIter_t>::const_iterator lEnd = theChildren.end();
1853+ for ( ; lIter != lEnd; ++lIter ){
1854+ (*lIter)->accept(v);
1855+ }
1856+
1857+ v.endVisit(*this);
1858+}
1859+
1860+JSONSerializeInternal::~JSONSerializeInternal() {}
1861+
1862+// </JSONSerializeInternal>
1863+
1864+
1865+
1866+}
1867+
1868+
1869
1870=== added file 'src/runtime/json/pregenerated/json.h'
1871--- src/runtime/json/pregenerated/json.h 1970-01-01 00:00:00 +0000
1872+++ src/runtime/json/pregenerated/json.h 2012-02-14 03:46:19 +0000
1873@@ -0,0 +1,114 @@
1874+/*
1875+ * Copyright 2006-2008 The FLWOR Foundation.
1876+ *
1877+ * Licensed under the Apache License, Version 2.0 (the "License");
1878+ * you may not use this file except in compliance with the License.
1879+ * You may obtain a copy of the License at
1880+ *
1881+ * http://www.apache.org/licenses/LICENSE-2.0
1882+ *
1883+ * Unless required by applicable law or agreed to in writing, software
1884+ * distributed under the License is distributed on an "AS IS" BASIS,
1885+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1886+ * See the License for the specific language governing permissions and
1887+ * limitations under the License.
1888+ */
1889+
1890+// ******************************************
1891+// * *
1892+// * THIS IS A GENERATED FILE. DO NOT EDIT! *
1893+// * SEE .xml FILE WITH SAME NAME *
1894+// * *
1895+// ******************************************
1896+#ifndef ZORBA_RUNTIME_JSON_JSON_H
1897+#define ZORBA_RUNTIME_JSON_JSON_H
1898+
1899+
1900+#include "common/shared_types.h"
1901+
1902+
1903+
1904+#include "runtime/base/narybase.h"
1905+
1906+
1907+namespace zorba {
1908+
1909+/**
1910+ *
1911+ * function for parsing strings into json-xdm
1912+ *
1913+ * Author: Zorba Team
1914+ */
1915+class JSONParseInternal : public NaryBaseIterator<JSONParseInternal, PlanIteratorState>
1916+{
1917+public:
1918+ SERIALIZABLE_CLASS(JSONParseInternal);
1919+
1920+ SERIALIZABLE_CLASS_CONSTRUCTOR2T(JSONParseInternal,
1921+ NaryBaseIterator<JSONParseInternal, PlanIteratorState>);
1922+
1923+ void serialize( ::zorba::serialization::Archiver& ar)
1924+ {
1925+ serialize_baseclass(ar,
1926+ (NaryBaseIterator<JSONParseInternal, PlanIteratorState>*)this);
1927+ }
1928+
1929+ JSONParseInternal(
1930+ static_context* sctx,
1931+ const QueryLoc& loc,
1932+ std::vector<PlanIter_t>& children)
1933+ :
1934+ NaryBaseIterator<JSONParseInternal, PlanIteratorState>(sctx, loc, children)
1935+ {}
1936+
1937+ virtual ~JSONParseInternal();
1938+
1939+ void accept(PlanIterVisitor& v) const;
1940+
1941+ bool nextImpl(store::Item_t& result, PlanState& aPlanState) const;
1942+};
1943+
1944+
1945+/**
1946+ *
1947+ * Function to serialize json/jsonml xdm to string
1948+ *
1949+ * Author: Zorba Team
1950+ */
1951+class JSONSerializeInternal : public NaryBaseIterator<JSONSerializeInternal, PlanIteratorState>
1952+{
1953+public:
1954+ SERIALIZABLE_CLASS(JSONSerializeInternal);
1955+
1956+ SERIALIZABLE_CLASS_CONSTRUCTOR2T(JSONSerializeInternal,
1957+ NaryBaseIterator<JSONSerializeInternal, PlanIteratorState>);
1958+
1959+ void serialize( ::zorba::serialization::Archiver& ar)
1960+ {
1961+ serialize_baseclass(ar,
1962+ (NaryBaseIterator<JSONSerializeInternal, PlanIteratorState>*)this);
1963+ }
1964+
1965+ JSONSerializeInternal(
1966+ static_context* sctx,
1967+ const QueryLoc& loc,
1968+ std::vector<PlanIter_t>& children)
1969+ :
1970+ NaryBaseIterator<JSONSerializeInternal, PlanIteratorState>(sctx, loc, children)
1971+ {}
1972+
1973+ virtual ~JSONSerializeInternal();
1974+
1975+ void accept(PlanIterVisitor& v) const;
1976+
1977+ bool nextImpl(store::Item_t& result, PlanState& aPlanState) const;
1978+};
1979+
1980+
1981+}
1982+#endif
1983+/*
1984+ * Local variables:
1985+ * mode: c++
1986+ * End:
1987+ */
1988
1989=== added file 'src/runtime/json/snelson.cpp'
1990--- src/runtime/json/snelson.cpp 1970-01-01 00:00:00 +0000
1991+++ src/runtime/json/snelson.cpp 2012-02-14 03:46:19 +0000
1992@@ -0,0 +1,515 @@
1993+/*
1994+ * Copyright 2006-2011 The FLWOR Foundation.
1995+ *
1996+ * Licensed under the Apache License, Version 2.0 (the "License");
1997+ * you may not use this file except in compliance with the License.
1998+ * You may obtain a copy of the License at
1999+ *
2000+ * http://www.apache.org/licenses/LICENSE-2.0
2001+ *
2002+ * Unless required by applicable law or agreed to in writing, software
2003+ * distributed under the License is distributed on an "AS IS" BASIS,
2004+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2005+ * See the License for the specific language governing permissions and
2006+ * limitations under the License.
2007+ */
2008+#include "stdafx.h"
2009+
2010+#include <sstream>
2011+
2012+#include <zorba/diagnostic_list.h>
2013+
2014+#include "runtime/json/json.h"
2015+#include "store/api/item_factory.h"
2016+#include "system/globalenv.h"
2017+#include "types/root_typemanager.h"
2018+#include "util/ascii_util.h"
2019+#include "util/cxx_util.h"
2020+#include "util/indent.h"
2021+#include "util/json_parser.h"
2022+#include "util/mem_streambuf.h"
2023+#include "util/omanip.h"
2024+#include "util/oseparator.h"
2025+#include "util/stl_util.h"
2026+
2027+#include "snelson.h"
2028+
2029+#define SNELSON_NS "http://john.snelson.org.uk/parsing-json-into-xquery"
2030+
2031+using namespace std;
2032+
2033+namespace zorba {
2034+
2035+///////////////////////////////////////////////////////////////////////////////
2036+
2037+static void add_type_attribute( store::Item *parent, char const *value ) {
2038+ store::Item_t junk_item, att_name, type_name, value_item;
2039+ GENV_ITEMFACTORY->createQName( att_name, "", "", "type" );
2040+ type_name = GENV_TYPESYSTEM.XS_UNTYPED_QNAME;
2041+ zstring value_string( value );
2042+ GENV_ITEMFACTORY->createString( value_item, value_string );
2043+ GENV_ITEMFACTORY->createAttributeNode(
2044+ junk_item, parent, att_name, type_name, value_item
2045+ );
2046+}
2047+
2048+#define ADD_TYPE_ATTRIBUTE(T) \
2049+ do { \
2050+ if ( needs_type_attribute ) { \
2051+ add_type_attribute( cur_item, T ); \
2052+ needs_type_attribute = false; \
2053+ } \
2054+ } while (0)
2055+
2056+static void add_item_element( item_stack_type &item_stack,
2057+ state_stack_type &state_stack,
2058+ store::Item_t &cur_item,
2059+ char const *type ) {
2060+ store::Item_t element_name, type_name;
2061+ zstring base_uri;
2062+ store::NsBindings ns_bindings;
2063+ GENV_ITEMFACTORY->createQName( element_name, SNELSON_NS, "", "item" );
2064+ type_name = GENV_TYPESYSTEM.XS_UNTYPED_QNAME;
2065+ GENV_ITEMFACTORY->createElementNode(
2066+ cur_item, item_stack.top(),
2067+ element_name, type_name, false, false, ns_bindings, base_uri
2068+ );
2069+ add_type_attribute( cur_item.getp(), type );
2070+ PUSH_ITEM( cur_item );
2071+}
2072+
2073+#define ADD_ITEM_ELEMENT(T) \
2074+ if ( !IN_STATE( in_array ) ) ; else \
2075+ add_item_element( item_stack, state_stack, cur_item, T )
2076+
2077+#define POP_ITEM_ELEMENT() \
2078+ if ( !IN_STATE( in_array ) ) ; else POP_ITEM()
2079+
2080+static void escape_json_chars( zstring *s ) {
2081+ ascii::replace_all( *s, "\"", 1, "\\\"", 2 );
2082+ ascii::replace_all( *s, "\\", 1, "\\\\", 2 );
2083+ ascii::replace_all( *s, "\b", 1, "\\b", 2 );
2084+ ascii::replace_all( *s, "\f", 1, "\\f", 2 );
2085+ ascii::replace_all( *s, "\n", 1, "\\n", 2 );
2086+ ascii::replace_all( *s, "\r", 1, "\\r", 2 );
2087+ ascii::replace_all( *s, "\t", 1, "\\t", 2 );
2088+}
2089+
2090+///////////////////////////////////////////////////////////////////////////////
2091+
2092+namespace snelson {
2093+
2094+void parse( json::parser &p, store::Item_t *result ) {
2095+ ZORBA_ASSERT( result );
2096+
2097+ store::Item_t cur_item, junk_item, value_item;
2098+ store::Item_t att_name, element_name, type_name;
2099+
2100+ zstring base_uri;
2101+ bool got_something = false;
2102+ item_stack_type item_stack;
2103+ bool needs_type_attribute = false;
2104+ bool next_string_is_key = false;
2105+ store::NsBindings ns_bindings;
2106+ state_stack_type state_stack;
2107+ zstring value;
2108+
2109+ json::token token;
2110+ while ( p.next( &token ) ) {
2111+ got_something = true;
2112+
2113+ if ( !*result ) {
2114+ GENV_ITEMFACTORY->createQName( element_name, SNELSON_NS, "", "json" );
2115+ type_name = GENV_TYPESYSTEM.XS_UNTYPED_QNAME;
2116+ GENV_ITEMFACTORY->createElementNode(
2117+ cur_item, nullptr,
2118+ element_name, type_name, false, false, ns_bindings, base_uri
2119+ );
2120+ *result = cur_item;
2121+ needs_type_attribute = true;
2122+ PUSH_ITEM( cur_item );
2123+ }
2124+
2125+ switch ( token.get_type() ) {
2126+
2127+ case '[':
2128+ if ( IN_STATE( in_object ) )
2129+ PUSH_ITEM( cur_item );
2130+ ADD_TYPE_ATTRIBUTE( "array" );
2131+ ADD_ITEM_ELEMENT( "array" );
2132+ PUSH_STATE( in_array );
2133+ break;
2134+
2135+ case '{':
2136+ if ( IN_STATE( in_object ) )
2137+ PUSH_ITEM( cur_item );
2138+ ADD_TYPE_ATTRIBUTE( "object" );
2139+ ADD_ITEM_ELEMENT( "object" );
2140+ PUSH_STATE( in_object );
2141+ next_string_is_key = true;
2142+ break;
2143+
2144+ case ']':
2145+ case '}':
2146+ POP_STATE();
2147+ POP_ITEM_ELEMENT();
2148+ if ( IN_STATE( in_object ) )
2149+ POP_ITEM();
2150+ break;
2151+
2152+ case ',':
2153+ next_string_is_key = IN_STATE( in_object );
2154+ break;
2155+
2156+ case json::token::number:
2157+ ADD_TYPE_ATTRIBUTE( "number" );
2158+ ADD_ITEM_ELEMENT( "number" );
2159+ value = token.get_value();
2160+ GENV_ITEMFACTORY->createTextNode( junk_item, cur_item, value );
2161+ POP_ITEM_ELEMENT();
2162+ break;
2163+
2164+ case json::token::string:
2165+ ADD_TYPE_ATTRIBUTE( "string" );
2166+ value = token.get_value();
2167+#if 0
2168+ escape_json_chars( &value );
2169+#endif
2170+
2171+ if ( next_string_is_key ) {
2172+ // <pair name="..." ...>
2173+ GENV_ITEMFACTORY->createQName( element_name, SNELSON_NS, "", "pair" );
2174+ type_name = GENV_TYPESYSTEM.XS_UNTYPED_QNAME;
2175+ GENV_ITEMFACTORY->createElementNode(
2176+ cur_item, item_stack.top(),
2177+ element_name, type_name, false, false, ns_bindings, base_uri
2178+ );
2179+
2180+ GENV_ITEMFACTORY->createQName( att_name, "", "", "name" );
2181+ type_name = GENV_TYPESYSTEM.XS_UNTYPED_QNAME;
2182+ GENV_ITEMFACTORY->createString( value_item, value );
2183+ GENV_ITEMFACTORY->createAttributeNode(
2184+ junk_item, cur_item, att_name, type_name, value_item
2185+ );
2186+
2187+ needs_type_attribute = true;
2188+ next_string_is_key = false;
2189+ } else {
2190+ ADD_ITEM_ELEMENT( "string" );
2191+ GENV_ITEMFACTORY->createTextNode( junk_item, cur_item, value );
2192+ POP_ITEM_ELEMENT();
2193+ }
2194+ break;
2195+
2196+ case 'F':
2197+ case 'T':
2198+ ADD_TYPE_ATTRIBUTE( "boolean" );
2199+ ADD_ITEM_ELEMENT( "boolean" );
2200+ value = token.get_type() == 'F' ? "false" : "true";
2201+ GENV_ITEMFACTORY->createTextNode( junk_item, cur_item, value );
2202+ POP_ITEM_ELEMENT();
2203+ break;
2204+
2205+ case json::token::json_null:
2206+ ADD_TYPE_ATTRIBUTE( "null" );
2207+ ADD_ITEM_ELEMENT( "null" );
2208+ POP_ITEM_ELEMENT();
2209+ break;
2210+
2211+ case ':':
2212+ case json::token::none:
2213+ break;
2214+
2215+ default:
2216+ assert( false );
2217+ } // switch
2218+ } // while
2219+ if ( !got_something )
2220+ throw XQUERY_EXCEPTION( zerr::ZJPE0009_ILLEGAL_EMPTY_STRING );
2221+}
2222+
2223+} // namespace snelson
2224+
2225+///////////////////////////////////////////////////////////////////////////////
2226+
2227+static void assert_json_type( json::type t, zstring const &s ) {
2228+ // Doing it this way uses the string data in-place with no copy.
2229+ mem_streambuf::char_type *const p =
2230+ const_cast<mem_streambuf::char_type*>( s.data() );
2231+ mem_streambuf buf( p, s.size() );
2232+ istringstream iss;
2233+ iss.ios::rdbuf( &buf );
2234+
2235+ json::lexer lex( iss );
2236+ json::token token;
2237+ try {
2238+ if ( lex.next( &token ) && json::map_type( token.get_type() ) == t )
2239+ return;
2240+ }
2241+ catch ( json::exception const& ) {
2242+ // do nothing
2243+ }
2244+ throw XQUERY_EXCEPTION(
2245+ zerr::ZJSE0008_BAD_VALUE,
2246+ ERROR_PARAMS( s, t )
2247+ );
2248+}
2249+
2250+static void require_attribute_value( store::Item_t const &element,
2251+ char const *att_name,
2252+ zstring *att_value ) {
2253+ if ( !get_attribute_value( element, att_name, att_value ) )
2254+ throw XQUERY_EXCEPTION(
2255+ zerr::ZJSE0002_ELEMENT_MISSING_ATTRIBUTE,
2256+ ERROR_PARAMS( element->getNodeName()->getStringValue(), att_name )
2257+ );
2258+}
2259+
2260+static json::type get_json_type( store::Item_t const &element,
2261+ bool allow_all_types = true ) {
2262+ zstring att_value;
2263+ require_attribute_value( element, "type", &att_value );
2264+ if ( att_value == "array" )
2265+ return json::array;
2266+ if ( att_value == "object" )
2267+ return json::object;
2268+ if ( allow_all_types ) {
2269+ if ( att_value == "boolean" )
2270+ return json::boolean;
2271+ if ( att_value == "null" )
2272+ return json::null;
2273+ if ( att_value == "number" )
2274+ return json::number;
2275+ if ( att_value == "string" )
2276+ return json::string;
2277+ }
2278+ throw XQUERY_EXCEPTION(
2279+ zerr::ZJSE0003_BAD_ATTRIBUTE_VALUE,
2280+ ERROR_PARAMS( att_value, "type" )
2281+ );
2282+}
2283+
2284+inline std::ostream& if_space_or_newline( std::ostream &o,
2285+ whitespace::type ws ) {
2286+ if ( ws == whitespace::some )
2287+ o << ' ';
2288+ else
2289+ o << if_emit( ws == whitespace::indent, '\n' );
2290+ return o;
2291+}
2292+DEF_OMANIP1( if_space_or_newline, whitespace::type )
2293+
2294+static ostream& serialize_begin( ostream &o, json::type t,
2295+ whitespace::type ws ) {
2296+ switch ( t ) {
2297+ case json::array :
2298+ o << '[' << if_emit( ws, ' ' );
2299+ break;
2300+ case json::object:
2301+ o << '{' << if_space_or_newline( ws ) << if_indent( ws, inc_indent );
2302+ break;
2303+ default:
2304+ /* suppress warning */;
2305+ }
2306+ return o;
2307+}
2308+DEF_OMANIP2( serialize_begin, json::type, whitespace::type )
2309+
2310+static ostream& serialize_end( ostream &o, json::type t, whitespace::type ws ) {
2311+ switch ( t ) {
2312+ case json::array:
2313+ o << if_emit( ws, ' ' ) << ']';
2314+ break;
2315+ case json::object:
2316+ o << if_space_or_newline( ws ) << if_indent( ws, dec_indent )
2317+ << if_indent( ws, indent ) << '}';
2318+ break;
2319+ default:
2320+ /* suppress warning */;
2321+ }
2322+ return o;
2323+}
2324+DEF_OMANIP2( serialize_end, json::type, whitespace::type )
2325+
2326+static ostream& serialize_boolean( ostream &o, zstring const &s ) {
2327+ assert_json_type( json::boolean, s );
2328+ return o << s;
2329+}
2330+DEF_OMANIP1( serialize_boolean, zstring const& )
2331+
2332+static ostream& serialize_number( ostream &o, zstring const &s ) {
2333+ assert_json_type( json::number, s );
2334+ return o << s;
2335+}
2336+DEF_OMANIP1( serialize_number, zstring const& )
2337+
2338+static ostream& serialize_string( ostream &o, zstring const &s ) {
2339+ zstring temp( s );
2340+ escape_json_chars( &temp );
2341+ temp.insert( (zstring::size_type)0, 1, '"' );
2342+ temp.append( 1, '"' );
2343+ assert_json_type( json::string, temp );
2344+ return o << temp;
2345+}
2346+DEF_OMANIP1( serialize_string, zstring const& )
2347+
2348+static ostream& serialize_children( ostream&, store::Item_t const&, json::type,
2349+ whitespace::type );
2350+DEF_OMANIP3( serialize_children, store::Item_t const&, json::type,
2351+ whitespace::type )
2352+
2353+static ostream& serialize_json_element( ostream &o,
2354+ store::Item_t const &element,
2355+ whitespace::type ws ) {
2356+ zstring const element_name( element->getNodeName()->getStringValue() );
2357+ if ( element_name != "json" )
2358+ throw XQUERY_EXCEPTION(
2359+ zerr::ZJSE0004_BAD_ELEMENT,
2360+ ERROR_PARAMS( element_name, "json" )
2361+ );
2362+
2363+ json::type const t = get_json_type( element, false );
2364+
2365+ return o
2366+ << serialize_begin( t, ws )
2367+ << serialize_children( element, t, ws )
2368+ << serialize_end( t, ws );
2369+}
2370+DEF_OMANIP2( serialize_json_element, store::Item_t const&, whitespace::type )
2371+
2372+static ostream& serialize_item_element( ostream &o,
2373+ store::Item_t const &element,
2374+ whitespace::type ws ) {
2375+ zstring const element_name( element->getNodeName()->getStringValue() );
2376+ if ( element_name != "item" )
2377+ throw XQUERY_EXCEPTION(
2378+ zerr::ZJSE0005_BAD_CHILD_ELEMENT,
2379+ ERROR_PARAMS( element_name, "array", "item" )
2380+ );
2381+
2382+ json::type const t = get_json_type( element );
2383+
2384+ return o
2385+ << serialize_begin( t, ws )
2386+ << serialize_children( element, t, ws )
2387+ << serialize_end( t, ws );
2388+}
2389+DEF_OMANIP2( serialize_item_element, store::Item_t const&, whitespace::type )
2390+
2391+static ostream& serialize_pair_element( ostream &o,
2392+ store::Item_t const &element,
2393+ whitespace::type ws ) {
2394+ zstring const element_name( element->getNodeName()->getStringValue() );
2395+ if ( element_name != "pair" )
2396+ throw XQUERY_EXCEPTION(
2397+ zerr::ZJSE0005_BAD_CHILD_ELEMENT,
2398+ ERROR_PARAMS( element_name, "object", "pair" )
2399+ );
2400+
2401+ zstring name_att_value;
2402+ require_attribute_value( element, "name", &name_att_value );
2403+ json::type const t = get_json_type( element );
2404+
2405+ return o
2406+ << if_indent( ws, indent ) << serialize_string( name_att_value )
2407+ << if_emit( ws, ' ' ) << ':' << if_emit( ws, ' ' )
2408+ << serialize_begin( t, ws )
2409+ << serialize_children( element, t, ws )
2410+ << serialize_end( t, ws );
2411+}
2412+DEF_OMANIP2( serialize_pair_element, store::Item_t const&, whitespace::type )
2413+
2414+static ostream& serialize_children( ostream &o, store::Item_t const &parent,
2415+ json::type parent_type,
2416+ whitespace::type ws ) {
2417+ if ( parent_type == json::null )
2418+ o << "null";
2419+ else {
2420+ oseparator sep;
2421+ if ( ws == whitespace::none )
2422+ sep.sep( "," );
2423+ else if ( ws == whitespace::some || parent_type == json::array )
2424+ sep.sep( ", " );
2425+ else
2426+ sep.sep( ",\n" );
2427+
2428+ store::Iterator_t i = parent->getChildren();
2429+ i->open();
2430+ store::Item_t child;
2431+ while ( i->next( child ) ) {
2432+
2433+ switch ( child->getNodeKind() ) {
2434+
2435+ case store::StoreConsts::elementNode:
2436+ o << sep;
2437+ switch ( parent_type ) {
2438+ case json::none:
2439+ o << serialize_json_element( child, ws );
2440+ break;
2441+ case json::array:
2442+ o << serialize_item_element( child, ws );
2443+ break;
2444+ case json::object:
2445+ o << serialize_pair_element( child, ws );
2446+ break;
2447+ default:
2448+ throw XQUERY_EXCEPTION(
2449+ zerr::ZJSE0006_NO_ELEMENT_CHILD,
2450+ ERROR_PARAMS( json::type_string_of[ parent_type ] )
2451+ );
2452+ }
2453+ break;
2454+
2455+ case store::StoreConsts::textNode:
2456+ o << sep;
2457+ switch ( parent_type ) {
2458+ case json::boolean:
2459+ o << serialize_boolean( child->getStringValue() );
2460+ break;
2461+ case json::number:
2462+ o << serialize_number( child->getStringValue() );
2463+ break;
2464+ case json::string:
2465+ o << serialize_string( child->getStringValue() );
2466+ break;
2467+ default:
2468+ throw XQUERY_EXCEPTION(
2469+ zerr::ZJSE0007_NO_TEXT_CHILD,
2470+ ERROR_PARAMS( json::type_string_of[ parent_type ] )
2471+ );
2472+ }
2473+ break;
2474+
2475+ default:
2476+ // do nothing
2477+ break;
2478+ } // switch
2479+ } // while
2480+ i->close();
2481+ }
2482+ return o;
2483+}
2484+
2485+///////////////////////////////////////////////////////////////////////////////
2486+
2487+namespace snelson {
2488+
2489+void serialize( ostream &o, store::Item_t const &item, whitespace::type ws ) {
2490+ switch ( item->getNodeKind() ) {
2491+ case store::StoreConsts::documentNode:
2492+ o << serialize_children( item, json::none, ws );
2493+ break;
2494+ case store::StoreConsts::elementNode:
2495+ o << serialize_json_element( item, ws );
2496+ break;
2497+ default:
2498+ throw XQUERY_EXCEPTION( zerr::ZJSE0001_NOT_DOCUMENT_OR_ELEMENT_NODE );
2499+ }
2500+}
2501+
2502+} // namespace snelson
2503+
2504+///////////////////////////////////////////////////////////////////////////////
2505+
2506+} // namespace zorba
2507+/* vim:set et sw=2 ts=2: */
2508
2509=== added file 'src/runtime/json/snelson.h'
2510--- src/runtime/json/snelson.h 1970-01-01 00:00:00 +0000
2511+++ src/runtime/json/snelson.h 2012-02-14 03:46:19 +0000
2512@@ -0,0 +1,41 @@
2513+/*
2514+ * Copyright 2006-2011 The FLWOR Foundation.
2515+ *
2516+ * Licensed under the Apache License, Version 2.0 (the "License");
2517+ * you may not use this file except in compliance with the License.
2518+ * You may obtain a copy of the License at
2519+ *
2520+ * http://www.apache.org/licenses/LICENSE-2.0
2521+ *
2522+ * Unless required by applicable law or agreed to in writing, software
2523+ * distributed under the License is distributed on an "AS IS" BASIS,
2524+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2525+ * See the License for the specific language governing permissions and
2526+ * limitations under the License.
2527+ */
2528+#include "stdafx.h"
2529+
2530+#ifndef ZORBA_RUNTIME_JSON_SNELSON_H
2531+#define ZORBA_RUNTIME_JSON_SNELSON_H
2532+
2533+#include <iostream>
2534+
2535+#include "store/api/item.h"
2536+#include "util/json_parser.h"
2537+
2538+#include "common.h"
2539+
2540+namespace zorba {
2541+namespace snelson {
2542+
2543+///////////////////////////////////////////////////////////////////////////////
2544+
2545+void parse( json::parser &p, store::Item_t *result );
2546+void serialize( std::ostream&, store::Item_t const &item, whitespace::type );
2547+
2548+///////////////////////////////////////////////////////////////////////////////
2549+
2550+} // namespace snelson
2551+} // namespace zorba
2552+#endif /* ZORBA_RUNTIME_JSON_SNELSON_H */
2553+/* vim:set et sw=2 ts=2: */
2554
2555=== added directory 'src/runtime/spec/json'
2556=== added file 'src/runtime/spec/json/json.xml'
2557--- src/runtime/spec/json/json.xml 1970-01-01 00:00:00 +0000
2558+++ src/runtime/spec/json/json.xml 2012-02-14 03:46:19 +0000
2559@@ -0,0 +1,52 @@
2560+<?xml version="1.0" encoding="UTF-8"?>
2561+
2562+<!--
2563+////////////////////////////////////////////////////////////////////////////////
2564+////////////////////////////////////////////////////////////////////////////////
2565+-->
2566+<zorba:iterators
2567+ xmlns:zorba="http://www.zorba-xquery.com"
2568+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2569+ xsi:schemaLocation="http://www.zorba-xquery.com ../runtime.xsd">
2570+
2571+<!--
2572+/*******************************************************************************
2573+*******************************************************************************/
2574+-->
2575+<zorba:iterator name="JSONParseInternal" arity="nary">
2576+
2577+ <zorba:description author="Zorba Team">
2578+ function for parsing strings into json-xdm
2579+ </zorba:description>
2580+
2581+ <zorba:function isDeterministic="true">
2582+ <zorba:signature localname="parse-internal" prefix="fn-zorba-json">
2583+ <zorba:param>xs:string</zorba:param>
2584+ <zorba:param>item()?</zorba:param>
2585+ <zorba:output>element()*</zorba:output>
2586+ </zorba:signature>
2587+ </zorba:function>
2588+
2589+</zorba:iterator>
2590+
2591+<!--
2592+/*******************************************************************************
2593+*******************************************************************************/
2594+-->
2595+<zorba:iterator name="JSONSerializeInternal" arity="nary">
2596+
2597+ <zorba:description author="Zorba Team">
2598+ Function to serialize json/jsonml xdm to string
2599+ </zorba:description>
2600+
2601+ <zorba:function isDeterministic="true">
2602+ <zorba:signature localname="serialize-internal" prefix="fn-zorba-json">
2603+ <zorba:param>item()*</zorba:param>
2604+ <zorba:param>item()?</zorba:param>
2605+ <zorba:output>xs:string</zorba:output>
2606+ </zorba:signature>
2607+ </zorba:function>
2608+
2609+</zorba:iterator>
2610+
2611+</zorba:iterators>
2612
2613=== modified file 'src/runtime/spec/mappings.xml'
2614--- src/runtime/spec/mappings.xml 2011-10-14 07:35:51 +0000
2615+++ src/runtime/spec/mappings.xml 2012-02-14 03:46:19 +0000
2616@@ -106,6 +106,10 @@
2617 define="ZORBA_STRING_FN_NS"
2618 prefix="fn-zorba-string"/>
2619
2620+ <zorba:namespace uri="http://www.zorba-xquery.com/modules/converters/json"
2621+ define="ZORBA_JSON_FN_NS"
2622+ prefix="fn-zorba-json"/>
2623+
2624 <zorba:namespace uri="http://www.zorba-xquery.com/modules/fetch"
2625 define="ZORBA_FETCH_FN_NS"
2626 prefix="fn-zorba-fetch"/>
2627
2628=== modified file 'src/runtime/visitors/pregenerated/planiter_visitor.h'
2629--- src/runtime/visitors/pregenerated/planiter_visitor.h 2012-01-11 17:30:25 +0000
2630+++ src/runtime/visitors/pregenerated/planiter_visitor.h 2012-02-14 03:46:19 +0000
2631@@ -251,6 +251,10 @@
2632
2633 class FunctionAnnotationsIterator;
2634
2635+ class JSONParseInternal;
2636+
2637+ class JSONSerializeInternal;
2638+
2639 class SqrtIterator;
2640
2641 class ExpIterator;
2642@@ -929,6 +933,12 @@
2643 virtual void beginVisit ( const FunctionAnnotationsIterator& ) = 0;
2644 virtual void endVisit ( const FunctionAnnotationsIterator& ) = 0;
2645
2646+ virtual void beginVisit ( const JSONParseInternal& ) = 0;
2647+ virtual void endVisit ( const JSONParseInternal& ) = 0;
2648+
2649+ virtual void beginVisit ( const JSONSerializeInternal& ) = 0;
2650+ virtual void endVisit ( const JSONSerializeInternal& ) = 0;
2651+
2652 virtual void beginVisit ( const SqrtIterator& ) = 0;
2653 virtual void endVisit ( const SqrtIterator& ) = 0;
2654
2655
2656=== modified file 'src/runtime/visitors/pregenerated/printer_visitor.cpp'
2657--- src/runtime/visitors/pregenerated/printer_visitor.cpp 2012-01-11 17:30:25 +0000
2658+++ src/runtime/visitors/pregenerated/printer_visitor.cpp 2012-02-14 03:46:19 +0000
2659@@ -50,6 +50,7 @@
2660 #include "runtime/function_item/function_item_iter.h"
2661 #include "runtime/indexing/ic_ddl.h"
2662 #include "runtime/introspection/sctx.h"
2663+#include "runtime/json/json.h"
2664 #include "runtime/maths/maths.h"
2665 #include "runtime/nodes/node_position.h"
2666 #include "runtime/nodes/nodes.h"
2667@@ -1650,6 +1651,34 @@
2668 // </FunctionAnnotationsIterator>
2669
2670
2671+// <JSONParseInternal>
2672+void PrinterVisitor::beginVisit ( const JSONParseInternal& a) {
2673+ thePrinter.startBeginVisit("JSONParseInternal", ++theId);
2674+ printCommons( &a, theId );
2675+ thePrinter.endBeginVisit( theId );
2676+}
2677+
2678+void PrinterVisitor::endVisit ( const JSONParseInternal& ) {
2679+ thePrinter.startEndVisit();
2680+ thePrinter.endEndVisit();
2681+}
2682+// </JSONParseInternal>
2683+
2684+
2685+// <JSONSerializeInternal>
2686+void PrinterVisitor::beginVisit ( const JSONSerializeInternal& a) {
2687+ thePrinter.startBeginVisit("JSONSerializeInternal", ++theId);
2688+ printCommons( &a, theId );
2689+ thePrinter.endBeginVisit( theId );
2690+}
2691+
2692+void PrinterVisitor::endVisit ( const JSONSerializeInternal& ) {
2693+ thePrinter.startEndVisit();
2694+ thePrinter.endEndVisit();
2695+}
2696+// </JSONSerializeInternal>
2697+
2698+
2699 // <SqrtIterator>
2700 void PrinterVisitor::beginVisit ( const SqrtIterator& a) {
2701 thePrinter.startBeginVisit("SqrtIterator", ++theId);
2702
2703=== modified file 'src/runtime/visitors/pregenerated/printer_visitor.h'
2704--- src/runtime/visitors/pregenerated/printer_visitor.h 2012-01-11 17:30:25 +0000
2705+++ src/runtime/visitors/pregenerated/printer_visitor.h 2012-02-14 03:46:19 +0000
2706@@ -379,6 +379,12 @@
2707 void beginVisit( const FunctionAnnotationsIterator& );
2708 void endVisit ( const FunctionAnnotationsIterator& );
2709
2710+ void beginVisit( const JSONParseInternal& );
2711+ void endVisit ( const JSONParseInternal& );
2712+
2713+ void beginVisit( const JSONSerializeInternal& );
2714+ void endVisit ( const JSONSerializeInternal& );
2715+
2716 void beginVisit( const SqrtIterator& );
2717 void endVisit ( const SqrtIterator& );
2718
2719
2720=== modified file 'src/unit_tests/CMakeLists.txt'
2721--- src/unit_tests/CMakeLists.txt 2012-02-02 09:56:52 +0000
2722+++ src/unit_tests/CMakeLists.txt 2012-02-14 03:46:19 +0000
2723@@ -19,6 +19,8 @@
2724 test_uri.cpp
2725 unique_ptr.cpp
2726 unit_tests.cpp
2727+ test_uri.cpp
2728+ json_parser.cpp
2729 )
2730
2731 IF (NOT ZORBA_NO_FULL_TEXT)
2732
2733=== added file 'src/unit_tests/json_parser.cpp'
2734--- src/unit_tests/json_parser.cpp 1970-01-01 00:00:00 +0000
2735+++ src/unit_tests/json_parser.cpp 2012-02-14 03:46:19 +0000
2736@@ -0,0 +1,636 @@
2737+/*
2738+ * Copyright 2006-2008 The FLWOR Foundation.
2739+ *
2740+ * Licensed under the Apache License, Version 2.0 (the "License");
2741+ * you may not use this file except in compliance with the License.
2742+ * You may obtain a copy of the License at
2743+ *
2744+ * http://www.apache.org/licenses/LICENSE-2.0
2745+ *
2746+ * Unless required by applicable law or agreed to in writing, software
2747+ * distributed under the License is distributed on an "AS IS" BASIS,
2748+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2749+ * See the License for the specific language governing permissions and
2750+ * limitations under the License.
2751+ */
2752+
2753+#include <sstream>
2754+
2755+#include "util/json_parser.h"
2756+
2757+using namespace std;
2758+using namespace zorba;
2759+using namespace zorba::json;
2760+
2761+///////////////////////////////////////////////////////////////////////////////
2762+
2763+static int failures;
2764+
2765+static bool assert_true( char const *expr, int line, bool result ) {
2766+ if ( !result ) {
2767+ cout << "FAILED, line " << line << ": " << expr << endl;
2768+ ++failures;
2769+ }
2770+ return result;
2771+}
2772+
2773+static void print_exception( char const *expr, int line,
2774+ std::exception const &e ) {
2775+ assert_true( expr, line, false );
2776+ cout << "+ exception: ";
2777+ if ( json::exception const *j = dynamic_cast<json::exception const*>( &e ) ) {
2778+ json::location const &loc = j->get_loc();
2779+ if ( loc.file() && *loc.file() )
2780+ cout << '"' << loc.file() << "\": ";
2781+ cout << loc.line();
2782+ if ( loc.column() )
2783+ cout << ',' << loc.column();
2784+ cout << ": ";
2785+ }
2786+ cout << e.what() << endl;
2787+}
2788+
2789+#define ASSERT_TRUE( EXPR ) assert_true( #EXPR, __LINE__, !!(EXPR) )
2790+
2791+#define ASSERT_EXCEPTION( EXPR, EXCEPTION ) \
2792+ try { EXPR; assert_true( #EXPR, __LINE__, false ); } \
2793+ catch ( EXCEPTION const& ) { }
2794+
2795+#define ASSERT_NO_EXCEPTION( EXPR ) \
2796+ try { EXPR; } \
2797+ catch ( std::exception const &e ) { print_exception( #EXPR, __LINE__, e ); } \
2798+ catch ( ... ) { assert_true( #EXPR, __LINE__, false ); }
2799+
2800+#define ASSERT_TRUE_AND_NO_EXCEPTION( EXPR ) \
2801+ try { ASSERT_TRUE( EXPR ); } \
2802+ catch ( std::exception const &e ) { print_exception( #EXPR, __LINE__, e ); } \
2803+ catch ( ... ) { assert_true( #EXPR, __LINE__, false ); }
2804+
2805+///////////////////////////////////////////////////////////////////////////////
2806+
2807+static void test_empty_stream() {
2808+ char const source[] = "";
2809+ istringstream iss( source );
2810+ parser p( iss );
2811+ token t;
2812+ ASSERT_NO_EXCEPTION( p.next( &t ) );
2813+}
2814+
2815+static void test_illegal_character() {
2816+ char const source[] = " x ";
2817+ istringstream iss( source );
2818+ lexer lex( iss );
2819+ token t;
2820+ ASSERT_EXCEPTION( lex.next( &t ), illegal_character );
2821+}
2822+
2823+static void test_illegal_codepoint() {
2824+ static char const *const sources[] = {
2825+ " \" \\u \" ",
2826+ " \" \\u0 \" ",
2827+ " \" \\u00 \" ",
2828+ " \" \\u000 \" ",
2829+ " \" \\uG \" ",
2830+ " \" \\u\" ",
2831+ 0
2832+ };
2833+
2834+ for ( char const *const *s = sources; *s; ++s ) {
2835+ istringstream iss( *s );
2836+ lexer lex( iss );
2837+ token t;
2838+ ASSERT_EXCEPTION( lex.next( &t ), illegal_codepoint );
2839+ }
2840+}
2841+
2842+static void test_illegal_escape() {
2843+ char const source[] = " \" \\x \" ";
2844+ istringstream iss( source );
2845+ lexer lex( iss );
2846+ token t;
2847+ ASSERT_EXCEPTION( lex.next( &t ), illegal_escape );
2848+}
2849+
2850+static void test_illegal_literal() {
2851+ static char const *const sources[] = {
2852+ " f ",
2853+ " fa ",
2854+ " fal ",
2855+ " fals ",
2856+ " falsee ",
2857+ " t ",
2858+ " tr ",
2859+ " tru ",
2860+ " truee ",
2861+ " n ",
2862+ " nu ",
2863+ " nul ",
2864+ " nulll ",
2865+ 0
2866+ };
2867+
2868+ for ( char const *const *s = sources; *s; ++s ) {
2869+ istringstream iss( *s );
2870+ lexer lex( iss );
2871+ token t;
2872+ ASSERT_EXCEPTION( lex.next( &t ), illegal_literal );
2873+ }
2874+}
2875+
2876+static void test_illegal_number() {
2877+}
2878+
2879+static void test_json_org_example() {
2880+ char const source[] =
2881+/* 1 */ "{" "\n"
2882+/* 2 */ " \"glossary\": {" "\n"
2883+/* 3 */ " \"title\": \"example glossary\"," "\n"
2884+/* 4 */ " \"GlossDiv\": {" "\n"
2885+/* 5 */ " \"title\": \"S\"," "\n"
2886+/* 6 */ " \"GlossList\": {" "\n"
2887+/* 7 */ " \"GlossEntry\": {" "\n"
2888+/* 8 */ " \"ID\": \"SGML\"," "\n"
2889+/* 9 */ " \"SortAs\": \"SGML\"," "\n"
2890+/* 10 */ " \"GlossTerm\": \"Standard Generalized Markup Language\"," "\n"
2891+/* 11 */ " \"Acronym\": \"SGML\"," "\n"
2892+/* 12 */ " \"Abbrev\": \"ISO 8879:1986\"," "\n"
2893+/* 13 */ " \"GlossDef\": {" "\n"
2894+/* 14 */ " \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\"," "\n"
2895+/* 15 */ " \"GlossSeeAlso\": [\"GML\", \"XML\"]" "\n"
2896+/* 16 */ " }," "\n"
2897+/* 17 */ " \"GlossSee\": \"markup\"" "\n"
2898+/* 18 */ " }" "\n"
2899+/* 19 */ " }" "\n"
2900+/* 20 */ " }" "\n"
2901+/* 21 */ " }" "\n"
2902+/* 22 */ "}" "\n" ;
2903+
2904+ istringstream iss( source );
2905+ parser p( iss );
2906+ token t;
2907+
2908+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 1: {
2909+ ASSERT_TRUE( t == token::begin_object );
2910+
2911+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 2: "glossary"
2912+ ASSERT_TRUE( t == token::string );
2913+
2914+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 2: :
2915+ ASSERT_TRUE( t == token::name_separator );
2916+
2917+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 2: {
2918+ ASSERT_TRUE( t == token::begin_object );
2919+
2920+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 3: "title"
2921+ ASSERT_TRUE( t == token::string );
2922+
2923+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 3: :
2924+ ASSERT_TRUE( t == token::name_separator );
2925+
2926+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 3: "example glossary"
2927+ ASSERT_TRUE( t == token::string );
2928+
2929+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 3: ,
2930+ ASSERT_TRUE( t == token::value_separator );
2931+
2932+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 4: "GlossDiv"
2933+ ASSERT_TRUE( t == token::string );
2934+
2935+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 4: :
2936+ ASSERT_TRUE( t == token::name_separator );
2937+
2938+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 4: {
2939+ ASSERT_TRUE( t == token::begin_object );
2940+
2941+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 5: "title"
2942+ ASSERT_TRUE( t == token::string );
2943+
2944+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 5: :
2945+ ASSERT_TRUE( t == token::name_separator );
2946+
2947+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 5: "S"
2948+ ASSERT_TRUE( t == token::string );
2949+
2950+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 5: ,
2951+ ASSERT_TRUE( t == token::value_separator );
2952+
2953+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 6: "GlossList"
2954+ ASSERT_TRUE( t == token::string );
2955+
2956+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 6: :
2957+ ASSERT_TRUE( t == token::name_separator );
2958+
2959+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 6: {
2960+ ASSERT_TRUE( t == token::begin_object );
2961+
2962+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 7: "GlossEntry"
2963+ ASSERT_TRUE( t == token::string );
2964+
2965+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 7: :
2966+ ASSERT_TRUE( t == token::name_separator );
2967+
2968+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 7: {
2969+ ASSERT_TRUE( t == token::begin_object );
2970+
2971+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 8: "ID"
2972+ ASSERT_TRUE( t == token::string );
2973+
2974+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 8: :
2975+ ASSERT_TRUE( t == token::name_separator );
2976+
2977+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 8: "SGML"
2978+ ASSERT_TRUE( t == token::string );
2979+
2980+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 8: ,
2981+ ASSERT_TRUE( t == token::value_separator );
2982+
2983+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 9: "SortAs"
2984+ ASSERT_TRUE( t == token::string );
2985+
2986+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 9: :
2987+ ASSERT_TRUE( t == token::name_separator );
2988+
2989+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 9: "SGML"
2990+ ASSERT_TRUE( t == token::string );
2991+
2992+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 9: ,
2993+ ASSERT_TRUE( t == token::value_separator );
2994+
2995+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 10: "GlossTerm"
2996+ ASSERT_TRUE( t == token::string );
2997+
2998+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 10: :
2999+ ASSERT_TRUE( t == token::name_separator );
3000+
3001+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 10: "Standard ..."
3002+ ASSERT_TRUE( t == token::string );
3003+
3004+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 10: ,
3005+ ASSERT_TRUE( t == token::value_separator );
3006+
3007+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 11: "Acronym"
3008+ ASSERT_TRUE( t == token::string );
3009+
3010+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 11: :
3011+ ASSERT_TRUE( t == token::name_separator );
3012+
3013+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 11: "SGML"
3014+ ASSERT_TRUE( t == token::string );
3015+
3016+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 11: ,
3017+ ASSERT_TRUE( t == token::value_separator );
3018+
3019+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 12: "Abbrev"
3020+ ASSERT_TRUE( t == token::string );
3021+
3022+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 12: :
3023+ ASSERT_TRUE( t == token::name_separator );
3024+
3025+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 12: "ISO 8879:1986"
3026+ ASSERT_TRUE( t == token::string );
3027+
3028+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 12: ,
3029+ ASSERT_TRUE( t == token::value_separator );
3030+
3031+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 13: "GlossDef"
3032+ ASSERT_TRUE( t == token::string );
3033+
3034+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 13: :
3035+ ASSERT_TRUE( t == token::name_separator );
3036+
3037+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 13: {
3038+ ASSERT_TRUE( t == token::begin_object );
3039+
3040+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 14: "para"
3041+ ASSERT_TRUE( t == token::string );
3042+
3043+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 14: :
3044+ ASSERT_TRUE( t == token::name_separator );
3045+
3046+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 14: "A meta-markup ..."
3047+ ASSERT_TRUE( t == token::string );
3048+
3049+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 14: ,
3050+ ASSERT_TRUE( t == token::value_separator );
3051+
3052+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 15: "GlossSeeAlso"
3053+ ASSERT_TRUE( t == token::string );
3054+
3055+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 15: :
3056+ ASSERT_TRUE( t == token::name_separator );
3057+
3058+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 15: [
3059+ ASSERT_TRUE( t == token::begin_array );
3060+
3061+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 15: "GML"
3062+ ASSERT_TRUE( t == token::string );
3063+
3064+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 15: ,
3065+ ASSERT_TRUE( t == token::value_separator );
3066+
3067+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 15: "XML"
3068+ ASSERT_TRUE( t == token::string );
3069+
3070+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 15: ]
3071+ ASSERT_TRUE( t == token::end_array );
3072+
3073+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 16: }
3074+ ASSERT_TRUE( t == token::end_object );
3075+
3076+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 16: ,
3077+ ASSERT_TRUE( t == token::value_separator );
3078+
3079+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 17: "GlossSee"
3080+ ASSERT_TRUE( t == token::string );
3081+
3082+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 17: :
3083+ ASSERT_TRUE( t == token::name_separator );
3084+
3085+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 17: "markup"
3086+ ASSERT_TRUE( t == token::string );
3087+
3088+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 18: }
3089+ ASSERT_TRUE( t == token::end_object );
3090+
3091+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 19: }
3092+ ASSERT_TRUE( t == token::end_object );
3093+
3094+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 20: }
3095+ ASSERT_TRUE( t == token::end_object );
3096+
3097+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 21: }
3098+ ASSERT_TRUE( t == token::end_object );
3099+
3100+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) ); // 22: }
3101+ ASSERT_TRUE( t == token::end_object );
3102+
3103+ ASSERT_TRUE( !p.next( &t ) );
3104+}
3105+
3106+static void test_lexer_array() {
3107+ char const source[] = "[ 1, \"2\", false, true, null ]";
3108+ istringstream iss( source );
3109+ lexer lex( iss );
3110+ token t;
3111+
3112+ ASSERT_TRUE( lex.next( &t ) );
3113+ ASSERT_TRUE( t == token::begin_array );
3114+
3115+ ASSERT_TRUE( lex.next( &t ) );
3116+ ASSERT_TRUE( t == token::number );
3117+ ASSERT_TRUE( t.get_value() == "1" );
3118+
3119+ ASSERT_TRUE( lex.next( &t ) );
3120+ ASSERT_TRUE( t == token::value_separator );
3121+
3122+ ASSERT_TRUE( lex.next( &t ) );
3123+ ASSERT_TRUE( t == token::string );
3124+ ASSERT_TRUE( t.get_value() == "2" );
3125+
3126+ ASSERT_TRUE( lex.next( &t ) );
3127+ ASSERT_TRUE( t == token::value_separator );
3128+
3129+ ASSERT_TRUE( lex.next( &t ) );
3130+ ASSERT_TRUE( t == token::json_false );
3131+
3132+ ASSERT_TRUE( lex.next( &t ) );
3133+ ASSERT_TRUE( t == token::value_separator );
3134+
3135+ ASSERT_TRUE( lex.next( &t ) );
3136+ ASSERT_TRUE( t == token::json_true );
3137+
3138+ ASSERT_TRUE( lex.next( &t ) );
3139+ ASSERT_TRUE( t == token::value_separator );
3140+
3141+ ASSERT_TRUE( lex.next( &t ) );
3142+ ASSERT_TRUE( t == token::json_null );
3143+
3144+ ASSERT_TRUE( lex.next( &t ) );
3145+ ASSERT_TRUE( t == token::end_array );
3146+
3147+ ASSERT_TRUE( !lex.next( &t ) );
3148+}
3149+
3150+static void test_lexer_object() {
3151+ char const source[] = "{ \"a\" : 1, \"b\" : \"2\" }";
3152+ istringstream iss( source );
3153+ lexer lex( iss );
3154+ token t;
3155+
3156+ ASSERT_TRUE( lex.next( &t ) );
3157+ ASSERT_TRUE( t == token::begin_object );
3158+
3159+ ASSERT_TRUE( lex.next( &t ) );
3160+ ASSERT_TRUE( t == token::string );
3161+
3162+ ASSERT_TRUE( lex.next( &t ) );
3163+ ASSERT_TRUE( t == token::name_separator );
3164+
3165+ ASSERT_TRUE( lex.next( &t ) );
3166+ ASSERT_TRUE( t == token::number );
3167+
3168+ ASSERT_TRUE( lex.next( &t ) );
3169+ ASSERT_TRUE( t == token::value_separator );
3170+
3171+ ASSERT_TRUE( lex.next( &t ) );
3172+ ASSERT_TRUE( t == token::string );
3173+
3174+ ASSERT_TRUE( lex.next( &t ) );
3175+ ASSERT_TRUE( t == token::name_separator );
3176+
3177+ ASSERT_TRUE( lex.next( &t ) );
3178+ ASSERT_TRUE( t == token::string );
3179+
3180+ ASSERT_TRUE( lex.next( &t ) );
3181+ ASSERT_TRUE( t == token::end_object );
3182+
3183+ ASSERT_TRUE( !lex.next( &t ) );
3184+}
3185+
3186+static void test_parser_array() {
3187+ char const source[] = "[ 1, \"2\", false, true, null ]";
3188+ istringstream iss( source );
3189+ parser p( iss );
3190+ token t;
3191+
3192+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3193+ ASSERT_TRUE( t == token::begin_array );
3194+
3195+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3196+ ASSERT_TRUE( t == token::number );
3197+ ASSERT_TRUE( t.get_value() == "1" );
3198+
3199+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3200+ ASSERT_TRUE( t == token::value_separator );
3201+
3202+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3203+ ASSERT_TRUE( t == token::string );
3204+ ASSERT_TRUE( t.get_value() == "2" );
3205+
3206+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3207+ ASSERT_TRUE( t == token::value_separator );
3208+
3209+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3210+ ASSERT_TRUE( t == token::json_false );
3211+
3212+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3213+ ASSERT_TRUE( t == token::value_separator );
3214+
3215+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3216+ ASSERT_TRUE( t == token::json_true );
3217+
3218+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3219+ ASSERT_TRUE( t == token::value_separator );
3220+
3221+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3222+ ASSERT_TRUE( t == token::json_null );
3223+
3224+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3225+ ASSERT_TRUE( t == token::end_array );
3226+
3227+ ASSERT_TRUE( !p.next( &t ) );
3228+}
3229+
3230+static void test_parser_object() {
3231+ char const source[] = "{ \"a\" : 1, \"b\" : \"2\" }";
3232+ istringstream iss( source );
3233+ parser p( iss );
3234+ token t;
3235+
3236+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3237+ ASSERT_TRUE( t == token::begin_object );
3238+
3239+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3240+ ASSERT_TRUE( t == token::string );
3241+
3242+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3243+ ASSERT_TRUE( t == token::name_separator );
3244+
3245+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3246+ ASSERT_TRUE( t == token::number );
3247+
3248+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3249+ ASSERT_TRUE( t == token::value_separator );
3250+
3251+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3252+ ASSERT_TRUE( t == token::string );
3253+
3254+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3255+ ASSERT_TRUE( t == token::name_separator );
3256+
3257+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3258+ ASSERT_TRUE( t == token::string );
3259+
3260+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3261+ ASSERT_TRUE( t == token::end_object );
3262+
3263+ ASSERT_TRUE( !p.next( &t ) );
3264+}
3265+
3266+static void test_unexpected_token() {
3267+ token t;
3268+ {
3269+ char const source[] = "{ 1 }";
3270+ istringstream iss( source );
3271+ parser p( iss );
3272+
3273+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3274+ ASSERT_TRUE( t == token::begin_object );
3275+ ASSERT_EXCEPTION( p.next( &t ), unexpected_token );
3276+ }
3277+ {
3278+ char const source[] = "{ \"a\" : 1, }";
3279+ istringstream iss( source );
3280+ parser p( iss );
3281+
3282+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3283+ ASSERT_TRUE( t == token::begin_object );
3284+
3285+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3286+ ASSERT_TRUE( t == token::string );
3287+
3288+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3289+ ASSERT_TRUE( t == token::name_separator );
3290+
3291+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3292+ ASSERT_TRUE( t == token::number );
3293+
3294+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3295+ ASSERT_TRUE( t == token::value_separator );
3296+
3297+ ASSERT_EXCEPTION( p.next( &t ), unexpected_token );
3298+ }
3299+ {
3300+ char const source[] = "{ \"t\" : true \"f\" : false }";
3301+ istringstream iss( source );
3302+ parser p( iss );
3303+
3304+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3305+ ASSERT_TRUE( t == token::begin_object );
3306+
3307+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3308+ ASSERT_TRUE( t == token::string );
3309+
3310+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3311+ ASSERT_TRUE( t == token::name_separator );
3312+
3313+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3314+ ASSERT_TRUE( t == token::json_true );
3315+
3316+ ASSERT_EXCEPTION( p.next( &t ), unexpected_token );
3317+ }
3318+ {
3319+ char const source[] = "[ 1";
3320+ istringstream iss( source );
3321+ parser p( iss );
3322+
3323+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3324+ ASSERT_TRUE( t == token::begin_array );
3325+
3326+ ASSERT_TRUE_AND_NO_EXCEPTION( p.next( &t ) );
3327+ ASSERT_TRUE( t == token::number );
3328+
3329+ ASSERT_EXCEPTION( p.next( &t ), unexpected_token );
3330+ }
3331+}
3332+
3333+static void test_unterminated_string() {
3334+ char const source[] = " \"hello ";
3335+ istringstream iss( source );
3336+ lexer lex( iss );
3337+ token t;
3338+
3339+ ASSERT_EXCEPTION( lex.next( &t ), unterminated_string );
3340+}
3341+
3342+///////////////////////////////////////////////////////////////////////////////
3343+
3344+namespace zorba {
3345+namespace UnitTests {
3346+
3347+int json_parser( int, char*[] ) {
3348+
3349+ // lexer-only tests
3350+ test_lexer_array();
3351+ test_lexer_object();
3352+ test_illegal_character();
3353+ test_illegal_codepoint();
3354+ test_illegal_escape();
3355+ test_illegal_literal();
3356+ test_illegal_number();
3357+ test_unterminated_string();
3358+
3359+ // parser tests
3360+ test_empty_stream();
3361+ test_parser_array();
3362+ test_parser_object();
3363+ test_unexpected_token();
3364+ test_json_org_example();
3365+
3366+ cout << failures << " test(s) failed\n";
3367+ return failures ? 1 : 0;
3368+}
3369+
3370+} // namespace UnitTests
3371+} // namespace zorba
3372+/* vim:set et sw=2 ts=2: */
3373
3374=== modified file 'src/unit_tests/unit_test_list.h'
3375--- src/unit_tests/unit_test_list.h 2012-02-02 09:56:52 +0000
3376+++ src/unit_tests/unit_test_list.h 2012-02-14 03:46:19 +0000
3377@@ -34,6 +34,7 @@
3378 /**
3379 * ADD NEW UNIT TESTS HERE
3380 */
3381+ int json_parser( int, char*[] );
3382
3383 void initializeTestList();
3384 };
3385
3386=== modified file 'src/unit_tests/unit_tests.cpp'
3387--- src/unit_tests/unit_tests.cpp 2012-02-02 09:56:52 +0000
3388+++ src/unit_tests/unit_tests.cpp 2012-02-14 03:46:19 +0000
3389@@ -39,6 +39,7 @@
3390 void initializeTestList() {
3391 libunittests["string"] = test_string;
3392 libunittests["uri"] = runUriTest;
3393+ libunittests["json_parser"] = json_parser;
3394 libunittests["unique_ptr"] = test_unique_ptr;
3395 #ifndef ZORBA_NO_FULL_TEXT
3396 libunittests["stemmer"] = test_stemmer;
3397
3398=== modified file 'src/util/CMakeLists.txt'
3399--- src/util/CMakeLists.txt 2011-07-18 14:25:21 +0000
3400+++ src/util/CMakeLists.txt 2012-02-14 03:46:19 +0000
3401@@ -20,6 +20,8 @@
3402 dir.cpp
3403 fs_util.cpp
3404 indent.cpp
3405+ json_parser.cpp
3406+ mem_streambuf.cpp
3407 regex.cpp
3408 string_util.cpp
3409 unicode_util.cpp
3410
3411=== added file 'src/util/json_parser.cpp'
3412--- src/util/json_parser.cpp 1970-01-01 00:00:00 +0000
3413+++ src/util/json_parser.cpp 2012-02-14 03:46:19 +0000
3414@@ -0,0 +1,662 @@
3415+/*
3416+ * Copyright 2006-2008 The FLWOR Foundation.
3417+ *
3418+ * Licensed under the Apache License, Version 2.0 (the "License");
3419+ * you may not use this file except in compliance with the License.
3420+ * You may obtain a copy of the License at
3421+ *
3422+ * http://www.apache.org/licenses/LICENSE-2.0
3423+ *
3424+ * Unless required by applicable law or agreed to in writing, software
3425+ * distributed under the License is distributed on an "AS IS" BASIS,
3426+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3427+ * See the License for the specific language governing permissions and
3428+ * limitations under the License.
3429+ */
3430+
3431+#include "diagnostics/assert.h"
3432+
3433+#include "ascii_util.h"
3434+#include "stl_util.h"
3435+#include "string_util.h"
3436+#include "utf8_util.h"
3437+
3438+#define DEBUG_JSON_PARSER 0
3439+
3440+#if DEBUG_JSON_PARSER
3441+# include "indent.h"
3442+#endif /* DEBUG_JSON_PARSER */
3443+
3444+#include "json_parser.h"
3445+
3446+using namespace std;
3447+
3448+namespace zorba {
3449+namespace json {
3450+
3451+///////////////////////////////////////////////////////////////////////////////
3452+
3453+char const *const type_string_of[] = {
3454+ "none",
3455+ "array",
3456+ "boolean",
3457+ "null",
3458+ "number",
3459+ "object",
3460+ "string"
3461+};
3462+
3463+type map_type( token::type tt ) {
3464+ switch ( tt ) {
3465+ case token::string:
3466+ return string;
3467+ case token::number:
3468+ return number;
3469+ case token::json_false:
3470+ case token::json_true:
3471+ return boolean;
3472+ case token::json_null:
3473+ return null;
3474+ default:
3475+ return none;
3476+ }
3477+}
3478+
3479+///////////////////////////////////////////////////////////////////////////////
3480+
3481+exception::exception( location const &loc, std::string const &message ) :
3482+ loc_( loc ), message_( message )
3483+{
3484+}
3485+
3486+exception::~exception() throw() {
3487+ // out-of-line since it's virtual
3488+}
3489+
3490+char const* exception::what() const throw() {
3491+ return message_.c_str();
3492+}
3493+
3494+illegal_character::illegal_character( location const &loc, char c ) :
3495+ exception( loc, BUILD_STRING( '\'', c, "': illegal character" ) ),
3496+ c_( c )
3497+{
3498+}
3499+
3500+illegal_character::~illegal_character() throw() {
3501+ // out-of-line since it's virtual
3502+}
3503+
3504+illegal_codepoint::illegal_codepoint( location const &loc,
3505+ token::value_type const &cp ) :
3506+ exception( loc, BUILD_STRING( '"', cp, "\": illegal codepoint" ) ),
3507+ codepoint_( cp )
3508+{
3509+}
3510+
3511+illegal_codepoint::~illegal_codepoint() throw() {
3512+ // out-of-line since it's virtual
3513+}
3514+
3515+illegal_escape::illegal_escape( location const &loc, char c ) :
3516+ exception( loc, BUILD_STRING( "\"\\", c, "\": illegal character escape" ) ),
3517+ esc_( c )
3518+{
3519+}
3520+
3521+illegal_escape::~illegal_escape() throw() {
3522+ // out-of-line since it's virtual
3523+}
3524+
3525+illegal_literal::illegal_literal( location const &loc ) :
3526+ exception( loc, "illegal literal" )
3527+{
3528+}
3529+
3530+illegal_literal::~illegal_literal() throw() {
3531+ // out-of-line since it's virtual
3532+}
3533+
3534+illegal_number::illegal_number( location const &loc ) :
3535+ exception( loc, "illegal number" )
3536+{
3537+}
3538+
3539+illegal_number::~illegal_number() throw() {
3540+ // out-of-line since it's virtual
3541+}
3542+
3543+unexpected_token::unexpected_token( token const &t ) :
3544+ exception( t.get_loc(), BUILD_STRING( '"', t, "\": unexpected token" ) ),
3545+ token_( t )
3546+{
3547+}
3548+
3549+unexpected_token::~unexpected_token() throw() {
3550+ // out-of-line since it's virtual
3551+}
3552+
3553+unterminated_string::unterminated_string( location const &loc ) :
3554+ exception( loc, "unterminated string" )
3555+{
3556+}
3557+
3558+unterminated_string::~unterminated_string() throw() {
3559+ // out-of-line since it's virtual
3560+}
3561+
3562+///////////////////////////////////////////////////////////////////////////////
3563+
3564+token::token() :
3565+ type_( none )
3566+{
3567+}
3568+
3569+ostream& operator<<( ostream &o, token::type tt ) {
3570+ switch ( tt ) {
3571+ case token::string : o << "string"; break;
3572+ case token::number : o << "number"; break;
3573+ case token::json_false: o << "false" ; break;
3574+ case token::json_null : o << "null" ; break;
3575+ case token::json_true : o << "true" ; break;
3576+ case token::none : o << "<none>"; break;
3577+ default : o << static_cast<char>( tt );
3578+ }
3579+ return o;
3580+}
3581+
3582+ostream& operator<<( ostream &o, token const &t ) {
3583+ switch ( t.get_type() ) {
3584+ case token::string: o << '"' << t.get_value() << '"'; break;
3585+ case token::number: o << t.get_value() ; break;
3586+ default : o << t.get_type() ;
3587+ }
3588+ return o;
3589+}
3590+
3591+///////////////////////////////////////////////////////////////////////////////
3592+
3593+lexer::lexer( istream &in ) :
3594+ in_( &in ),
3595+ line_( 1 ),
3596+ col_( 1 )
3597+{
3598+}
3599+
3600+bool lexer::get_char( char *c ) {
3601+ char const temp = in_->get();
3602+ if ( in_->good() ) {
3603+ if ( temp == '\n' )
3604+ ++line_, col_ = 1;
3605+ else
3606+ ++col_;
3607+ if ( c )
3608+ *c = temp;
3609+ return true;
3610+ }
3611+ return false;
3612+}
3613+
3614+bool lexer::peek_char( char *c ) {
3615+ *c = in_->peek();
3616+ return in_->good();
3617+}
3618+
3619+bool lexer::next( token *t ) {
3620+ while ( true ) {
3621+ cur_loc_ = cur_loc();
3622+ char c;
3623+ if ( !get_char( &c ) )
3624+ return false;
3625+ switch ( c ) {
3626+ case ' ':
3627+ case '\n':
3628+ case '\r':
3629+ case '\t':
3630+ continue;
3631+ case '"':
3632+ t->type_ = token::string;
3633+ t->loc_ = cur_loc_;
3634+ parse_string( &t->value_ );
3635+ return true;
3636+ case '-':
3637+ case '0':
3638+ case '1':
3639+ case '2':
3640+ case '3':
3641+ case '4':
3642+ case '5':
3643+ case '6':
3644+ case '7':
3645+ case '8':
3646+ case '9':
3647+ t->type_ = token::number;
3648+ t->loc_ = cur_loc_;
3649+ parse_number( c, &t->value_ );
3650+ return true;
3651+ case 'f':
3652+ case 'n':
3653+ case 't':
3654+ t->type_ = parse_literal( c, &t->value_ );
3655+ t->loc_ = cur_loc_;
3656+ return true;
3657+ case '[':
3658+ case '{':
3659+ case ']':
3660+ case '}':
3661+ case ':':
3662+ case ',':
3663+ t->type_ = static_cast<token::type>( c );
3664+ t->loc_ = cur_loc_;
3665+ return true;
3666+ default:
3667+ throw illegal_character( cur_loc_, c );
3668+ }
3669+ } // while
3670+}
3671+
3672+unicode::code_point lexer::parse_codepoint() {
3673+ static char const hex_digits[] = "0123456789ABCDEF";
3674+
3675+ zstring cp_string( "\\u" ); // needed only for error message
3676+
3677+ unicode::code_point cp = 0;
3678+ for ( int i = 1; i <= 4; ++i ) {
3679+ char c;
3680+ if ( !get_char( &c ) || !ascii::is_xdigit( c ) )
3681+ throw illegal_codepoint( cur_loc_, cp_string );
3682+ cp_string += c;
3683+ c = ascii::to_upper( c );
3684+ char const *const p = std::strchr( hex_digits, c );
3685+ assert( p );
3686+ cp = (cp << 4) | (p - hex_digits);
3687+ }
3688+ return cp;
3689+}
3690+
3691+token::type lexer::parse_literal( char first_c, token::value_type *value ) {
3692+ static token::value_type const false_value( "false" );
3693+ static token::value_type const null_value ( "null" );
3694+ static token::value_type const true_value ( "true" );
3695+
3696+ token::type tt;
3697+ switch ( first_c ) {
3698+ case 'f': *value = false_value; tt = token::json_false; break;
3699+ case 'n': *value = null_value ; tt = token::json_null ; break;
3700+ case 't': *value = true_value ; tt = token::json_true ; break;
3701+ default : assert( false );
3702+ }
3703+
3704+ char c;
3705+ for ( char const *s = value->c_str(); *++s; ) {
3706+ if ( !get_char( &c ) || c != *s )
3707+ throw illegal_literal( cur_loc_ );
3708+ }
3709+ if ( peek_char( &c ) && ascii::is_alnum( c ) )
3710+ throw illegal_literal( cur_loc_ );
3711+
3712+ return tt;
3713+}
3714+
3715+void lexer::parse_number( char first_c, token::value_type *value ) {
3716+ value->clear();
3717+
3718+ // <number> ::= [-] <int> [<frac>] [<exp>]
3719+ char c = first_c;
3720+ if ( c == '-' ) {
3721+ *value += c;
3722+ if ( !get_char( &c ) )
3723+ throw illegal_number( cur_loc_ );
3724+ }
3725+
3726+ // <int> := '0' | <1-9> <digit>*
3727+ if ( !ascii::is_digit( c ) )
3728+ throw illegal_number( cur_loc_ );
3729+ *value += c;
3730+ if ( c == '0' ) {
3731+ if ( !get_char( &c ) )
3732+ return;
3733+ } else {
3734+ while ( true ) {
3735+ if ( !get_char( &c ) )
3736+ return;
3737+ if ( !ascii::is_digit( c ) )
3738+ break;
3739+ *value += c;
3740+ }
3741+ }
3742+
3743+ // <frac> ::= '.' <digit>+
3744+ if ( c == '.' ) {
3745+ *value += c;
3746+ if ( !get_char( &c ) || !ascii::is_digit( c ) )
3747+ throw illegal_number( cur_loc_ );
3748+ *value += c;
3749+ while ( true ) {
3750+ if ( !get_char( &c ) )
3751+ return;
3752+ if ( !ascii::is_digit( c ) )
3753+ break;
3754+ *value += c;
3755+ }
3756+ }
3757+
3758+ // <exp> ::= <e> [<sign>] <digit>+
3759+ // <e> ::= 'e' | 'E'
3760+ // <sign> ::= '-' | '+'
3761+ if ( c == 'e' || c == 'E' ) {
3762+ *value += c;
3763+ if ( !get_char( &c ) )
3764+ throw illegal_number( cur_loc_ );
3765+ if ( c == '+' || c == '-' ) {
3766+ *value += c;
3767+ if ( !get_char( &c ) )
3768+ throw illegal_number( cur_loc_ );
3769+ }
3770+ if ( !ascii::is_digit( c ) )
3771+ throw illegal_number( cur_loc_ );
3772+ *value += c;
3773+ while ( true ) {
3774+ if ( !get_char( &c ) )
3775+ return;
3776+ if ( !ascii::is_digit( c ) )
3777+ break;
3778+ *value += c;
3779+ }
3780+ }
3781+
3782+ in_->putback( c );
3783+}
3784+
3785+void lexer::parse_string( token::value_type *value ) {
3786+ value->clear();
3787+ bool got_backslash = false;
3788+ location const start_loc( cur_loc_ );
3789+
3790+ while ( true ) {
3791+ cur_loc_ = cur_loc();
3792+ char c;
3793+ if ( !get_char( &c ) )
3794+ throw unterminated_string( start_loc );
3795+ if ( got_backslash ) {
3796+ got_backslash = false;
3797+ switch ( c ) {
3798+ case '"':
3799+ case '/':
3800+ case '\\':
3801+ *value += c;
3802+ break;
3803+ case 'b':
3804+ *value += '\b';
3805+ break;
3806+ case 'f':
3807+ *value += '\f';
3808+ break;
3809+ case 'n':
3810+ *value += '\n';
3811+ break;
3812+ case 'r':
3813+ *value += '\r';
3814+ break;
3815+ case 't':
3816+ *value += '\t';
3817+ break;
3818+ case 'u':
3819+ utf8::encode( parse_codepoint(), value );
3820+ break;
3821+ default:
3822+ throw illegal_escape( cur_loc_, c );
3823+ }
3824+ continue;
3825+ }
3826+
3827+ switch ( c ) {
3828+ case '\\':
3829+ got_backslash = true;
3830+ break;
3831+ case '"':
3832+ return;
3833+ default:
3834+ *value += c;
3835+ }
3836+ } // while
3837+}
3838+
3839+void lexer::set_loc( char const *file, line_type line, column_type col ) {
3840+ if ( file )
3841+ file_ = file;
3842+ line_ = line;
3843+ col_ = col;
3844+}
3845+
3846+///////////////////////////////////////////////////////////////////////////////
3847+
3848+#if DEBUG_JSON_PARSER
3849+
3850+ostream& operator<<( ostream &o, parser::state s ) {
3851+ static char const *const string_of[] = {
3852+ "A0", "A1", "A2",
3853+ "E0", "E1",
3854+ "J0", "J1",
3855+ "M0", "M1",
3856+ "O0", "O1", "O2",
3857+ "P0", "P1",
3858+ "V0"
3859+ };
3860+ return o << string_of[ s ];
3861+}
3862+
3863+static void throw_unexpected_token( int line, token const &t ) {
3864+ try {
3865+ throw unexpected_token( t );
3866+ }
3867+ catch ( exception const &e ) {
3868+ cerr << line << ": " << e.what() << endl;
3869+ throw;
3870+ }
3871+}
3872+
3873+bool parser::get_token_debug( int line, token *t ) {
3874+ bool const got_token = get_token( t );
3875+ cout << line << ": get_token => " << *t << endl;
3876+ return got_token;
3877+}
3878+
3879+bool parser::matches_token_debug( int line, token::type tt, token *t ) {
3880+ bool const matched = matches_token( tt, t );
3881+ cout << line << ": token " << *t << " matches " << tt << " => " << (matched ? 'T' : 'F') << endl;
3882+ return matched;
3883+}
3884+
3885+token::type parser::peek_token_debug( int line ) {
3886+ token::type const tt = peek_token();
3887+ cout << line << ": peek_token => " << peeked_token_ << endl;
3888+ return tt;
3889+}
3890+
3891+void parser::require_token_debug( int line, token::type tt, token *t ) {
3892+ if ( !get_token_debug( line, t ) || t->get_type() != tt )
3893+ throw_unexpected_token( line, *t );
3894+}
3895+
3896+# define GET_TOKEN(T) get_token_debug( __LINE__, T )
3897+# define MATCHES_TOKEN(TT,T) matches_token_debug( __LINE__, TT, T )
3898+# define PEEK_TOKEN() peek_token_debug( __LINE__ )
3899+# define REQUIRE_TOKEN(TT,T) require_token_debug( __LINE__, TT, T )
3900+# define THROW_UNEXPECTED_TOKEN(T) throw_unexpected_token( __LINE__, T )
3901+
3902+# define GOTO_STATE(S) \
3903+ if (0) ; else { \
3904+ state_ = (S); \
3905+ cout << __LINE__ << ':' << indent << "GOTO_STATE( " << state_ << " )" << endl; \
3906+ continue; \
3907+ }
3908+
3909+# define PUSH_STATE(S) \
3910+ if (0) ; else { \
3911+ state_stack_.push(S); \
3912+ cout << __LINE__ << ':' << indent << "PUSH_STATE( " << (S) << " )" << endl << inc_indent; \
3913+ }
3914+
3915+# define POP_STATE() \
3916+ if (0) ; else { \
3917+ state_ = ztd::pop_stack( state_stack_ ); \
3918+ cout << __LINE__ << ':' << indent << "POP_STATE() => " << state_ << endl << dec_indent; \
3919+ }
3920+
3921+#else
3922+
3923+# define GET_TOKEN(T) get_token( T )
3924+# define MATCHES_TOKEN(TT,T) matches_token( TT, T )
3925+# define PEEK_TOKEN() peek_token()
3926+# define REQUIRE_TOKEN(TT,T) require_token( TT, T )
3927+# define THROW_UNEXPECTED_TOKEN(T) throw unexpected_token( T )
3928+
3929+# define GOTO_STATE(S) { state_ = (S); continue; }
3930+# define PUSH_STATE(S) state_stack_.push(S)
3931+# define POP_STATE() state_ = ztd::pop_stack( state_stack_ )
3932+
3933+#endif /* DEBUG_JSON_PARSER */
3934+
3935+///////////////////////////////////////////////////////////////////////////////
3936+
3937+parser::parser( istream &in ) : lexer_( in ) {
3938+#if DEBUG_JSON_PARSER
3939+ get_indent( cout ) = 0;
3940+#endif /* DEBUG_JSON_PARSER */
3941+ PUSH_STATE( J0 );
3942+}
3943+
3944+bool parser::get_token( token *t ) {
3945+ if ( peeked_token_ ) {
3946+ *t = peeked_token_;
3947+ peeked_token_.clear();
3948+ return true;
3949+ }
3950+ t->clear();
3951+ return lexer_.next( t );
3952+}
3953+
3954+bool parser::matches_token( token::type tt, token *t ) {
3955+ if ( peek_token() == tt )
3956+ return get_token( t );
3957+ *t = peeked_token_;
3958+ return false;
3959+}
3960+
3961+token::type parser::peek_token() {
3962+ if ( !peeked_token_ )
3963+ lexer_.next( &peeked_token_ );
3964+ return peeked_token_.get_type();
3965+}
3966+
3967+#if ! DEBUG_JSON_PARSER
3968+void parser::require_token( token::type tt, token *t ) {
3969+ if ( !get_token( t ) || t->get_type() != tt )
3970+ THROW_UNEXPECTED_TOKEN( *t );
3971+}
3972+#endif /* DEBUG_JSON_PARSER */
3973+
3974+bool parser::next( token *t ) {
3975+ if ( state_stack_.empty() )
3976+ return false;
3977+ POP_STATE();
3978+ while ( true ) {
3979+ switch ( state_ ) {
3980+
3981+ // <JSON> ::= <Array> | <Object>
3982+ case J0: PUSH_STATE( J1 );
3983+ switch ( PEEK_TOKEN() ) {
3984+ case token::begin_array : GOTO_STATE( A0 );
3985+ case token::begin_object: GOTO_STATE( O0 );
3986+ case token::none : break;
3987+ default: THROW_UNEXPECTED_TOKEN( peeked_token_ );
3988+ }
3989+ case J1: return false;
3990+
3991+ // <Array> ::= '[' <Element>* ']'
3992+ case A0: REQUIRE_TOKEN( token::begin_array, t );
3993+ PUSH_STATE( A1 );
3994+ return true;
3995+ case A1: if ( MATCHES_TOKEN( token::end_array, t ) )
3996+ return true;
3997+ PUSH_STATE( A2 );
3998+ GOTO_STATE( E0 );
3999+ case A2: REQUIRE_TOKEN( token::end_array, t );
4000+ return true;
4001+
4002+ // <Element> ::= <Value> [ ',' <Element> ]
4003+ case E0: PUSH_STATE( E1 );
4004+ GOTO_STATE( V0 );
4005+ case E1: if ( MATCHES_TOKEN( token::value_separator, t ) ) {
4006+ PUSH_STATE( E0 );
4007+ return true;
4008+ }
4009+ POP_STATE();
4010+ continue;
4011+
4012+ // <Object> ::= '{' <Member>* '}'
4013+ case O0: REQUIRE_TOKEN( token::begin_object, t );
4014+ PUSH_STATE( O1 );
4015+ return true;
4016+ case O1: if ( MATCHES_TOKEN( token::end_object, t ) )
4017+ return true;
4018+ PUSH_STATE( O2 );
4019+ GOTO_STATE( M0 );
4020+ case O2: REQUIRE_TOKEN( token::end_object, t );
4021+ return true;
4022+
4023+ // <Member> ::= <Pair> [ ',' <Member> ]
4024+ case M0: PUSH_STATE( M1 );
4025+ GOTO_STATE( P0 );
4026+ case M1: if ( MATCHES_TOKEN( token::value_separator, t ) ) {
4027+ PUSH_STATE( M0 );
4028+ return true;
4029+ }
4030+ POP_STATE();
4031+ continue;
4032+
4033+ // <Pair> ::= <String> ':' <Value>
4034+ case P0: REQUIRE_TOKEN( token::string, t );
4035+ PUSH_STATE( P1 );
4036+ return true;
4037+ case P1: REQUIRE_TOKEN( token::name_separator, t );
4038+ PUSH_STATE( V0 );
4039+ return true;
4040+
4041+ // <Value> ::= <Array> | <Object> | <String> | <Number>
4042+ // | false | null | true
4043+ case V0: switch ( PEEK_TOKEN() ) {
4044+ case token::begin_array:
4045+ GOTO_STATE( A0 );
4046+ case token::begin_object:
4047+ GOTO_STATE( O0 );
4048+ case token::string:
4049+ case token::number:
4050+ case token::json_false:
4051+ case token::json_null:
4052+ case token::json_true:
4053+ GET_TOKEN( t );
4054+ return true;
4055+ default:
4056+ THROW_UNEXPECTED_TOKEN( peeked_token_ );
4057+ }
4058+ } // switch ( state_ )
4059+ } // while
4060+}
4061+
4062+token::type parser::peek( token *t ) {
4063+ if ( token::type const tt = PEEK_TOKEN() ) {
4064+ if ( t )
4065+ *t = peeked_token_;
4066+ return tt;
4067+ }
4068+ return token::none;
4069+}
4070+
4071+///////////////////////////////////////////////////////////////////////////////
4072+
4073+} // namespace json
4074+} // namespace zorba
4075+
4076+/* vim:set et sw=2 ts=2: */
4077
4078=== added file 'src/util/json_parser.h'
4079--- src/util/json_parser.h 1970-01-01 00:00:00 +0000
4080+++ src/util/json_parser.h 2012-02-14 03:46:19 +0000
4081@@ -0,0 +1,570 @@
4082+/*
4083+ * Copyright 2006-2008 The FLWOR Foundation.
4084+ *
4085+ * Licensed under the Apache License, Version 2.0 (the "License");
4086+ * you may not use this file except in compliance with the License.
4087+ * You may obtain a copy of the License at
4088+ *
4089+ * http://www.apache.org/licenses/LICENSE-2.0
4090+ *
4091+ * Unless required by applicable law or agreed to in writing, software
4092+ * distributed under the License is distributed on an "AS IS" BASIS,
4093+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4094+ * See the License for the specific language governing permissions and
4095+ * limitations under the License.
4096+ */
4097+
4098+#ifndef ZORBA_JSON_PARSER_H
4099+#define ZORBA_JSON_PARSER_H
4100+
4101+#include <zorba/config.h>
4102+
4103+#include <exception>
4104+#include <iostream>
4105+#include <stack>
4106+#include <string>
4107+
4108+#include <zorba/internal/diagnostic.h>
4109+
4110+#include "zorbatypes/zstring.h"
4111+
4112+#include "cxx_util.h"
4113+#include "unicode_util.h"
4114+
4115+namespace zorba {
4116+namespace json {
4117+
4118+///////////////////////////////////////////////////////////////////////////////
4119+
4120+typedef internal::diagnostic::location location;
4121+
4122+///////////////////////////////////////////////////////////////////////////////
4123+
4124+/**
4125+ * A JSON %type is the type of JSON data. This isn't used by the lexer or
4126+ * parser implementation at all, but it's handy.
4127+ */
4128+enum type {
4129+ none, // meaning "not set" as opposed to "null"
4130+ array,
4131+ boolean,
4132+ null,
4133+ number,
4134+ object,
4135+ string
4136+};
4137+extern char const *const type_string_of[];
4138+
4139+inline std::ostream& operator<<( std::ostream &o, type t ) {
4140+ return o << type_string_of[ t ];
4141+}
4142+
4143+/**
4144+ * A JSON %token. Tokens have a type, location at which they were found, and
4145+ * sometimes a value.
4146+ *
4147+ * See: "RFC 4627: The application/json Media Type for JavaScript Object
4148+ * Notation (JSON)."
4149+ */
4150+class token {
4151+ // see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2333.html
4152+ struct pointer_conversion { int valid; };
4153+ typedef int pointer_conversion::*explicit_bool;
4154+public:
4155+ typedef zstring value_type;
4156+
4157+ /**
4158+ * The types of tokens in JSON. The first 6 constants have values that
4159+ * correspond to the actual structural characters used by JSON; the rest were
4160+ * assigned non-standard, mnemonic values for convenience.
4161+ */
4162+ enum type {
4163+ none,
4164+ begin_array = '[',
4165+ begin_object = '{',
4166+ end_array = ']',
4167+ end_object = '}',
4168+ name_separator = ':',
4169+ value_separator = ',',
4170+ string = 'S',
4171+ number = 'N',
4172+ json_false = 'F',
4173+ json_null = '0',
4174+ json_true = 'T',
4175+ };
4176+
4177+ /**
4178+ * Default constructor.
4179+ */
4180+ token();
4181+
4182+ /**
4183+ * Clears this %token.
4184+ */
4185+ void clear() {
4186+ type_ = none;
4187+ value_.clear();
4188+ }
4189+
4190+ /**
4191+ * Gets the location at which this %token was found.
4192+ *
4193+ * @return Returns said location.
4194+ */
4195+ location const& get_loc() const {
4196+ return loc_;
4197+ }
4198+
4199+ /**
4200+ * Gets the type of this %token.
4201+ *
4202+ * @return Returns said type.
4203+ */
4204+ type get_type() const {
4205+ return type_;
4206+ }
4207+
4208+ /**
4209+ * Gets the value of this %token, if any. Only %token types string, number,
4210+ * false, null, and true have a value.
4211+ *
4212+ * @return Returns said value or the empty string.
4213+ */
4214+ value_type const& get_value() const {
4215+ return value_;
4216+ }
4217+
4218+ /**
4219+ * Conversion to \c bool.
4220+ *
4221+ * @return Returns \c true only if this token's type is not \c none.
4222+ */
4223+ operator explicit_bool() const {
4224+ return type_ ? &pointer_conversion::valid : nullptr;
4225+ }
4226+
4227+private:
4228+ location loc_;
4229+ type type_;
4230+ value_type value_;
4231+
4232+ friend class lexer;
4233+};
4234+
4235+/**
4236+ * Map a token's type to a JSON type.
4237+ *
4238+ * @param tt The token::type to map.
4239+ * @return Returns the corresponding JSON type or \c none if \a tt doesn't map.
4240+ */
4241+type map_type( token::type tt );
4242+
4243+/**
4244+ * Emits the given token type to an ostream.
4245+ *
4246+ * @param o The ostream to emit to.
4247+ * @param tt The token type to emit.
4248+ * @return Returns \a o.
4249+ */
4250+std::ostream& operator<<( std::ostream &o, token::type tt );
4251+
4252+/**
4253+ * Emits the given token to an ostream.
4254+ *
4255+ * @param o The ostream to emit to.
4256+ * @param t The token to emit.
4257+ * @return Returns \a o.
4258+ */
4259+std::ostream& operator<<( std::ostream &o, token const &t );
4260+
4261+/**
4262+ * Compares two tokens for equality.
4263+ *
4264+ * @param t1 The first token.
4265+ * @param t2 The second token.
4266+ * @return Returns \c true only if the two tokens' types and values are equal.
4267+ */
4268+inline bool operator==( token const &t1, token const &t2 ) {
4269+ return t1.get_type() == t2.get_type() && t1.get_value() == t2.get_value();
4270+}
4271+
4272+/**
4273+ * Compares a token's type to another type for equality.
4274+ *
4275+ * @param t The token whose type to compare.
4276+ * @param tt The type to compare to.
4277+ * @return Returns \c true only if the token's type equals \a tt.
4278+ */
4279+inline bool operator==( token const &t, token::type tt ) {
4280+ return t.get_type() == tt;
4281+}
4282+
4283+/**
4284+ * Compares a token's type to another type for equality.
4285+ *
4286+ * @param tt The type to compare.
4287+ * @param t The token whose type to compare to.
4288+ * @return Returns \c true only if \a tt equals the token's type.
4289+ */
4290+inline bool operator==( token::type tt, token const &t ) {
4291+ return t == tt;
4292+}
4293+
4294+/**
4295+ * Compares a token's value to a C string for equality.
4296+ *
4297+ * @param t The token whose value to compare.
4298+ * @param value The value to compare to.
4299+ * @return Returns \c true only if the token's value equals \a value.
4300+ */
4301+inline bool operator==( token const &t, char const *value ) {
4302+ return t.get_value() == value;
4303+}
4304+
4305+/**
4306+ * Compares a C string to a token's value for equality.
4307+ *
4308+ * @param value The value to compare.
4309+ * @param t The token whose value to compare to.
4310+ * @return Returns \c true only if \a value equals the token's value.
4311+ */
4312+inline bool operator==( char const *value, token const &t ) {
4313+ return t == value;
4314+}
4315+
4316+/**
4317+ * Compares two tokens for inequality.
4318+ *
4319+ * @param t1 The first token.
4320+ * @param t2 The second token.
4321+ * @return Returns \c true if either the two tokens' types or values are not
4322+ * equal.
4323+ */
4324+inline bool operator!=( token const &t1, token const &t2 ) {
4325+ return !(t1 == t2);
4326+}
4327+
4328+/**
4329+ * Compares a token's type to another type for inequality.
4330+ *
4331+ * @param t The token whose type to compare.
4332+ * @param tt The type to compare to.
4333+ * @return Returns \c true only if the token's type is not equal to \a tt.
4334+ */
4335+inline bool operator!=( token const &t, token::type tt ) {
4336+ return !(t == tt);
4337+}
4338+
4339+/**
4340+ * Compares a token's type to another type for inequality.
4341+ *
4342+ * @param tt The type to compare.
4343+ * @param t The token whose type to compare to.
4344+ * @return Returns \c true only if \a tt is not equal to the token's type.
4345+ */
4346+inline bool operator!=( token::type tt, token const &t ) {
4347+ return !(tt == t);
4348+}
4349+
4350+/**
4351+ * Compares a token's value to a C string for inequality.
4352+ *
4353+ * @param t The token whose value to compare.
4354+ * @param value The value to compare to.
4355+ * @return Returns \c true only if the token's value is not equal to \a value.
4356+ */
4357+inline bool operator!=( token const &t, char const *value ) {
4358+ return !(t == value);
4359+}
4360+
4361+/**
4362+ * Compares a token's value to a C string for inequality.
4363+ *
4364+ * @param value The value to compare.
4365+ * @param t The token whose value to compare to.
4366+ * @return Returns \c true only if \a value is not equal to the token's value.
4367+ */
4368+inline bool operator!=( char const *value, token const &t ) {
4369+ return !(value == t);
4370+}
4371+
4372+///////////////////////////////////////////////////////////////////////////////
4373+
4374+/**
4375+ * An %exception is the root of the JSON %exception hierarchy.
4376+ */
4377+class exception : public std::exception {
4378+public:
4379+ ~exception() throw();
4380+
4381+ /**
4382+ * Gets the location in the JSON source whence this exception was thrown.
4383+ */
4384+ location const& get_loc() const {
4385+ return loc_;
4386+ }
4387+
4388+ // inherited
4389+ char const* what() const throw();
4390+
4391+protected:
4392+ exception( location const &loc, std::string const &message );
4393+
4394+private:
4395+ location loc_;
4396+ std::string message_;
4397+};
4398+
4399+/**
4400+ * This exception is thrown when an illegal character is encountered in a JSON
4401+ * data stream.
4402+ */
4403+class illegal_character : public exception {
4404+public:
4405+ illegal_character( location const &loc, char c );
4406+ ~illegal_character() throw();
4407+
4408+ /**
4409+ * Gets the illegal character.
4410+ *
4411+ * @return Returns said character.
4412+ */
4413+ char get_char() const {
4414+ return c_;
4415+ }
4416+
4417+private:
4418+ char c_;
4419+};
4420+
4421+/**
4422+ * This exception is thrown when an illegal Unicode code-point escape sequence
4423+ * (\uHHHH) is encountered.
4424+ */
4425+class illegal_codepoint : public exception {
4426+public:
4427+ illegal_codepoint( location const &loc, token::value_type const &cp );
4428+ ~illegal_codepoint() throw();
4429+
4430+ /**
4431+ * Gets the illegal code-point.
4432+ *
4433+ * @return Returns said code-point.
4434+ */
4435+ token::value_type get_codepoint() const {
4436+ return codepoint_;
4437+ }
4438+
4439+private:
4440+ token::value_type codepoint_;
4441+};
4442+
4443+/**
4444+ * This exception is thrown when an illegal character follows a backslash
4445+ * (escape) within a string literal. The legal escape characters are:
4446+ * ["/\bfnrtu].
4447+ */
4448+class illegal_escape : public exception {
4449+public:
4450+ illegal_escape( location const &loc, char escape );
4451+ ~illegal_escape() throw();
4452+
4453+ /**
4454+ * Gets the illegal escape character.
4455+ *
4456+ * @return Returns said character.
4457+ */
4458+ char get_escape() const {
4459+ return esc_;
4460+ }
4461+
4462+private:
4463+ char esc_;
4464+};
4465+
4466+/**
4467+ * This exception is thrown when a literal other than \c false, \c null, or
4468+ * \c true is encountered.
4469+ */
4470+class illegal_literal : public exception {
4471+public:
4472+ illegal_literal( location const &loc );
4473+ ~illegal_literal() throw();
4474+};
4475+
4476+/**
4477+ * This exception is thrown when an illegal number is encountered.
4478+ */
4479+class illegal_number : public exception {
4480+public:
4481+ illegal_number( location const &loc );
4482+ ~illegal_number() throw();
4483+};
4484+
4485+/**
4486+ * This exception is thrown when an unexpected token is encountered.
4487+ */
4488+class unexpected_token : public exception {
4489+public:
4490+ unexpected_token( token const &t );
4491+ ~unexpected_token() throw();
4492+
4493+ /**
4494+ * Gets the unexpected token
4495+ *
4496+ * @return Returns said token.
4497+ */
4498+ token const& get_token() const {
4499+ return token_;
4500+ }
4501+
4502+private:
4503+ token token_;
4504+};
4505+
4506+/**
4507+ * This exception is thrown when an EOF is encountered before a string's
4508+ * terminating quote.
4509+ */
4510+class unterminated_string : public exception {
4511+public:
4512+ unterminated_string( location const &loc );
4513+ ~unterminated_string() throw();
4514+};
4515+
4516+///////////////////////////////////////////////////////////////////////////////
4517+
4518+/**
4519+ * A %lexer extracts JSON tokens from an istream.
4520+ */
4521+class lexer {
4522+public:
4523+ typedef location::line_type line_type;
4524+ typedef location::column_type column_type;
4525+
4526+ /**
4527+ * Constructs a %lexer on the given istream.
4528+ *
4529+ * @param in The istream to read from.
4530+ */
4531+ lexer( std::istream &in );
4532+
4533+ /**
4534+ * Gets the next token, if any.
4535+ *
4536+ * @param result A pointer to the token to get into.
4537+ * @return Returns \c true only if there was a next token.
4538+ * @throws exception upon error.
4539+ */
4540+ bool next( token *result );
4541+
4542+ /**
4543+ * Sets the file location.
4544+ *
4545+ * @param file The source file name.
4546+ * @param line The source line number.
4547+ * @param col The source column number.
4548+ */
4549+ void set_loc( char const *file, line_type line, column_type col );
4550+
4551+private:
4552+ location cur_loc() const {
4553+ return location( file_, line_, col_ );
4554+ }
4555+
4556+ bool get_char( char* = nullptr );
4557+ bool peek_char( char* );
4558+ unicode::code_point parse_codepoint();
4559+ token::type parse_literal( char, token::value_type* );
4560+ void parse_number( char, token::value_type* );
4561+ void parse_string( token::value_type* );
4562+
4563+ std::istream *in_;
4564+ std::string file_;
4565+ line_type line_;
4566+ column_type col_;
4567+ location cur_loc_;
4568+};
4569+
4570+///////////////////////////////////////////////////////////////////////////////
4571+
4572+/**
4573+ * A %parser extracts JSON tokens from an istream while checking to ensure the
4574+ * token sequence is valid.
4575+ */
4576+class parser {
4577+public:
4578+ typedef lexer::line_type line_type;
4579+ typedef lexer::column_type column_type;
4580+
4581+ /**
4582+ * Constructs a %parser on the given istream.
4583+ *
4584+ * @param in The istream to read from.
4585+ */
4586+ parser( std::istream &in );
4587+
4588+ /**
4589+ * Gets the next token, if any.
4590+ *
4591+ * @param result A pointer to the token to receive the token.
4592+ * @return Returns \c true only if there was a next token.
4593+ * @throws exception upon error.
4594+ */
4595+ bool next( token *result );
4596+
4597+ /**
4598+ * Peeks at the next token, if any.
4599+ *
4600+ * @param result A pointer to the token to receive the token, if any.
4601+ * @return Returns the type of the peeked token.
4602+ * @throws exception upon error.
4603+ */
4604+ token::type peek( token *result = nullptr );
4605+
4606+ /**
4607+ * Sets the file location.
4608+ *
4609+ * @param file The source file name.
4610+ * @param line The source line number.
4611+ * @param col The source column number.
4612+ */
4613+ void set_loc( char const *file, line_type line, column_type col ) {
4614+ lexer_.set_loc( file, line, col );
4615+ }
4616+
4617+private:
4618+ enum state {
4619+ A0, A1, A2, // Array
4620+ E0, E1, // Element
4621+ J0, J1, // JSON
4622+ M0, M1, // Member
4623+ O0, O1, O2, // Object
4624+ P0, P1, // Pair
4625+ V0 // Value
4626+ };
4627+
4628+ friend std::ostream& operator<<( std::ostream&, state );
4629+
4630+ bool get_token( token* );
4631+ bool get_token_debug( int, token* );
4632+ bool matches_token( token::type, token* );
4633+ bool matches_token_debug( int, token::type, token* );
4634+ token::type peek_token();
4635+ token::type peek_token_debug( int );
4636+ void require_token( token::type, token* );
4637+ void require_token_debug( int, token::type, token* );
4638+
4639+ lexer lexer_;
4640+ token peeked_token_;
4641+ std::stack<state> state_stack_;
4642+ state state_;
4643+};
4644+
4645+///////////////////////////////////////////////////////////////////////////////
4646+
4647+} // namespace json
4648+} // namespace zorba
4649+
4650+#endif /* ZORBA_JSON_PARSER_H */
4651+/* vim:set et sw=2 ts=2: */
4652
4653=== added file 'src/util/mem_streambuf.cpp'
4654--- src/util/mem_streambuf.cpp 1970-01-01 00:00:00 +0000
4655+++ src/util/mem_streambuf.cpp 2012-02-14 03:46:19 +0000
4656@@ -0,0 +1,119 @@
4657+/*
4658+ * Copyright 2006-2008 The FLWOR Foundation.
4659+ *
4660+ * Licensed under the Apache License, Version 2.0 (the "License");
4661+ * you may not use this file except in compliance with the License.
4662+ * You may obtain a copy of the License at
4663+ *
4664+ * http://www.apache.org/licenses/LICENSE-2.0
4665+ *
4666+ * Unless required by applicable law or agreed to in writing, software
4667+ * distributed under the License is distributed on an "AS IS" BASIS,
4668+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4669+ * See the License for the specific language governing permissions and
4670+ * limitations under the License.
4671+ */
4672+
4673+#include <cstring> /* for memcpy(3) */
4674+
4675+#include "diagnostics/assert.h"
4676+
4677+#include "cxx_util.h"
4678+#include "mem_streambuf.h"
4679+
4680+using namespace std;
4681+
4682+namespace zorba {
4683+
4684+///////////////////////////////////////////////////////////////////////////////
4685+
4686+mem_streambuf::mem_streambuf() {
4687+ set( nullptr, nullptr );
4688+}
4689+
4690+mem_streambuf::mem_streambuf( char_type *begin, char_type *end ) {
4691+ set( begin, end );
4692+}
4693+
4694+mem_streambuf::mem_streambuf( char_type *begin, off_type size ) {
4695+ set( begin, size );
4696+}
4697+
4698+mem_streambuf::int_type mem_streambuf::overflow( int_type c ) {
4699+ if ( traits_type::eq_int_type( c, traits_type::eof() ) )
4700+ return traits_type::not_eof( c );
4701+ if ( pptr() >= epptr() )
4702+ return traits_type::eof();
4703+ *pptr() = traits_type::to_char_type( c );
4704+ pbump( 1 );
4705+ return c;
4706+}
4707+
4708+mem_streambuf::int_type mem_streambuf::pbackfail( int_type c ) {
4709+ if ( !traits_type::eq_int_type( c, traits_type::eof() ) ) {
4710+ *pptr() = traits_type::to_int_type( c );
4711+ pbump( -1 );
4712+ }
4713+ return traits_type::to_int_type( *pptr() );
4714+}
4715+
4716+mem_streambuf::pos_type mem_streambuf::seekoff( off_type off,
4717+ ios_base::seekdir dir,
4718+ ios_base::openmode ) {
4719+ switch ( dir ) {
4720+ case ios_base::beg:
4721+ our_setg( begin_ + off );
4722+ break;
4723+ case ios_base::cur:
4724+ our_setg( gptr() + off );
4725+ break;
4726+ case ios_base::end:
4727+ our_setg( end_ + off );
4728+ break;
4729+ default:
4730+ ZORBA_ASSERT( false );
4731+ }
4732+ return off;
4733+}
4734+
4735+mem_streambuf::pos_type mem_streambuf::seekpos( pos_type pos,
4736+ ios_base::openmode mode ) {
4737+ return seekoff( pos, ios_base::beg, mode );
4738+}
4739+
4740+void mem_streambuf::set( char_type *begin, char_type *end ) {
4741+ begin_ = begin;
4742+ end_ = end;
4743+ our_setg( begin );
4744+ our_setp( end );
4745+}
4746+
4747+streamsize mem_streambuf::showmanyc() {
4748+ return egptr() - gptr();
4749+}
4750+
4751+mem_streambuf::int_type mem_streambuf::underflow() {
4752+ return gptr() < egptr() ?
4753+ traits_type::to_int_type( *gptr() ) : traits_type::eof();
4754+}
4755+
4756+streamsize mem_streambuf::xsgetn( char_type *buf, std::streamsize size ) {
4757+ streamsize const remaining = showmanyc();
4758+ if ( size > remaining )
4759+ size = remaining;
4760+ ::memcpy( buf, gptr(), size );
4761+ return size;
4762+}
4763+
4764+streamsize mem_streambuf::xsputn( char_type const *buf, streamsize size ) {
4765+ streamsize const remaining = epptr() - pptr();
4766+ if ( size > remaining )
4767+ size = remaining;
4768+ ::memcpy( pptr(), buf, size );
4769+ return size;
4770+}
4771+
4772+///////////////////////////////////////////////////////////////////////////////
4773+
4774+} // namespace zorba
4775+/* vim:set et sw=2 ts=2: */
4776
4777=== added file 'src/util/mem_streambuf.h'
4778--- src/util/mem_streambuf.h 1970-01-01 00:00:00 +0000
4779+++ src/util/mem_streambuf.h 2012-02-14 03:46:19 +0000
4780@@ -0,0 +1,108 @@
4781+/*
4782+ * Copyright 2006-2008 The FLWOR Foundation.
4783+ *
4784+ * Licensed under the Apache License, Version 2.0 (the "License");
4785+ * you may not use this file except in compliance with the License.
4786+ * You may obtain a copy of the License at
4787+ *
4788+ * http://www.apache.org/licenses/LICENSE-2.0
4789+ *
4790+ * Unless required by applicable law or agreed to in writing, software
4791+ * distributed under the License is distributed on an "AS IS" BASIS,
4792+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4793+ * See the License for the specific language governing permissions and
4794+ * limitations under the License.
4795+ */
4796+
4797+#ifndef ZORBA_MMAP_STREAMBUF_H
4798+#define ZORBA_MMAP_STREAMBUF_H
4799+
4800+#include <streambuf>
4801+
4802+namespace zorba {
4803+
4804+///////////////////////////////////////////////////////////////////////////////
4805+
4806+/**
4807+ * A %mem_streambuf is-a std::streambuf for a fixed-size chunk of memory.
4808+ */
4809+class mem_streambuf : public std::streambuf {
4810+public:
4811+ typedef std::streambuf::char_type char_type;
4812+ typedef std::streambuf::int_type int_type;
4813+ typedef std::streambuf::off_type off_type;
4814+ typedef std::streambuf::pos_type pos_type;
4815+ typedef std::streambuf::traits_type traits_type;
4816+
4817+ /**
4818+ * Default constructor.
4819+ */
4820+ mem_streambuf();
4821+
4822+ /**
4823+ * Constructs a %mem_streambuf.
4824+ *
4825+ * @param begin A pointer to the beginning of the memory chunk.
4826+ * @param end A pointer to one past the end of the memory chunk.
4827+ */
4828+ mem_streambuf( char_type *begin, char_type *end );
4829+
4830+ /**
4831+ * Constructs a %mem_streambuf.
4832+ *
4833+ * @param begin A pointer to the beginning of the memory chunk.
4834+ * @param size The size of the memory chunk.
4835+ */
4836+ mem_streambuf( char_type *begin, off_type size );
4837+
4838+ /**
4839+ * Sets the memory chunk.
4840+ *
4841+ * @param begin A pointer to the beginning of the memory chunk.
4842+ * @param end A pointer to one past the end of the memory chunk.
4843+ */
4844+ void set( char_type *begin, char_type *end );
4845+
4846+ /**
4847+ * Sets the memory chunk.
4848+ *
4849+ * @param begin A pointer to the beginning of the memory chunk.
4850+ * @param size The size of the memory chunk.
4851+ */
4852+ void set( char_type *begin, off_type size );
4853+
4854+protected:
4855+ int_type overflow( int_type c );
4856+ int_type pbackfail( int_type c );
4857+ pos_type seekoff( off_type, std::ios_base::seekdir, std::ios_base::openmode );
4858+ pos_type seekpos( pos_type, std::ios_base::openmode );
4859+ std::streamsize showmanyc();
4860+ int_type underflow();
4861+ std::streamsize xsgetn( char_type*, std::streamsize );
4862+ std::streamsize xsputn( char_type const*, std::streamsize );
4863+
4864+private:
4865+ char_type *begin_, *end_;
4866+
4867+ void our_setg( char_type *ptr ) {
4868+ setg( begin_, ptr, end_ );
4869+ }
4870+
4871+ void our_setp( char_type *ptr ) {
4872+ setp( ptr, end_ );
4873+ }
4874+
4875+ // forbid
4876+ mem_streambuf( mem_streambuf const& );
4877+ mem_streambuf& operator=( mem_streambuf const& );
4878+};
4879+
4880+inline void mem_streambuf::set( char_type *begin, off_type size ) {
4881+ set( begin, begin + size );
4882+}
4883+
4884+///////////////////////////////////////////////////////////////////////////////
4885+
4886+} // namespace zorba
4887+#endif /* ZORBA_MMAP_STREAMBUF_H */
4888+/* vim:set et sw=2 ts=2: */
4889
4890=== modified file 'src/util/omanip.h'
4891--- src/util/omanip.h 2011-06-14 17:26:33 +0000
4892+++ src/util/omanip.h 2012-02-14 03:46:19 +0000
4893@@ -25,127 +25,275 @@
4894 ///////////////////////////////////////////////////////////////////////////////
4895
4896 /**
4897- * An omanip1 is a class for assisting in the creation of ostream manipulators
4898+ * An %omanip1 is a class for assisting in the creation of ostream manipulators
4899 * by storing a pointer to a function and its argument to be called later via
4900 * operator<<().
4901 *
4902 * See also: "Standard C++ IOStreams and Locales," Angelika Langer and Klaus
4903 * Kreft, Addison-Wesley, pp. 179-191.
4904 */
4905-template<typename Arg1Type> class omanip1 {
4906+template<typename Arg1Type>
4907+class omanip1 {
4908 public:
4909
4910 /**
4911- * The signature of functions this omanip1 can handle.
4912+ * The signature of functions this %omanip1 can handle.
4913 */
4914 typedef std::ostream& (*func_type)( std::ostream&, Arg1Type );
4915
4916 /**
4917- * Constructs an omanip1.
4918+ * Constructs an %omanip1.
4919 *
4920- * @param f The function to call when this omanip1 is inserted into an
4921- * ostream.
4922- * @param arg1 The argument to be passed to the function.
4923+ * @param f The function to call when this %omanip1 is inserted into an
4924+ * ostream.
4925+ * @param a1 The argument to be passed to the function.
4926 */
4927- omanip1( func_type f, Arg1Type arg1 ) :
4928- f_( f ), arg1_( arg1 )
4929+ omanip1( func_type f, Arg1Type a1 ) :
4930+ f_( f ), a1_( a1 )
4931 {
4932 }
4933
4934 /**
4935- * Inserts the given omanip1 into the given ostream. This has the effect of
4936- * calling the function and argument bound to the omanip1 at the time of its
4937+ * Inserts the given %omanip1 into the given ostream. This has the effect of
4938+ * calling the function and argument bound to the %omanip1 at the time of its
4939 * construction.
4940 *
4941 * @param o The ostream to insert into.
4942- * @param m The omanip1 to insert.
4943+ * @param m The %omanip1 to insert.
4944 */
4945 friend std::ostream& operator<<( std::ostream &o, omanip1 const &m ) {
4946- return (*m.f_)( o, m.arg1_ );
4947+ if ( o.good() )
4948+ (*m.f_)( o, m.a1_ );
4949+ return o;
4950 }
4951
4952 private:
4953 func_type const f_;
4954- Arg1Type const arg1_;
4955+ Arg1Type const a1_;
4956 };
4957
4958 /**
4959 * Defines an ostream manipulator "thunk" function that calls an existing
4960 * non-manipulator function having the same name.
4961 *
4962- * @param FN_NAME The name of the existing function.
4963- * @param ARG1_TYPE The type of the non-ostream argument.
4964- * @param ARG1_NAME The name of the non-ostream argument.
4965+ * @param FN_NAME The name of the existing function.
4966+ * @param ARG1_T The type of the non-ostream argument.
4967 */
4968-#define DEF_OMANIP1(FN_NAME,ARG1_TYPE,ARG1_NAME) \
4969- inline omanip1<ARG1_TYPE> \
4970- FN_NAME( ARG1_TYPE ARG1_NAME ) { \
4971- return omanip1<ARG1_TYPE>( FN_NAME, ARG1_NAME ); \
4972+#define DEF_OMANIP1(FN_NAME,ARG1_T) \
4973+ inline omanip1<ARG1_T> \
4974+ FN_NAME( ARG1_T a1 ) { \
4975+ return omanip1<ARG1_T>( FN_NAME, a1 ); \
4976 }
4977
4978 ///////////////////////////////////////////////////////////////////////////////
4979
4980 /**
4981- * An omanip2 is a class for assisting in the creation of ostream manipulators
4982+ * An %omanip2 is a class for assisting in the creation of ostream manipulators
4983 * by storing a pointer to a function and its arguments to be called later via
4984 * operator<<().
4985 *
4986 * See also: "Standard C++ IOStreams and Locales," Angelika Langer and Klaus
4987 * Kreft, Addison-Wesley, pp. 179-191.
4988 */
4989-template<typename Arg1Type, typename Arg2Type> class omanip2 {
4990+template<typename Arg1Type,typename Arg2Type>
4991+class omanip2 {
4992 public:
4993
4994 /**
4995- * The signature of functions this omanip2 can handle.
4996+ * The signature of functions this %omanip2 can handle.
4997 */
4998 typedef std::ostream& (*func_type)( std::ostream&, Arg1Type, Arg2Type );
4999
5000 /**
The diff has been truncated for viewing.

Subscribers

People subscribed via source and target branches