What's new in version 1.1

Compatible with both wxWidgets 2.8 and 2.9

The new version is compatible with both wxWidgets 2.8 and 2.9. Tried using wxGTK 2.8.10 and 2.9.1 (daily snapshot wxWidgets-2009-10-17)

A new AsXxxxxx() function

The wxJSONValue::AsXxxxx() function can be used to get the value of a JSON value but you have first to check if it is of the expected type. So you would probably write code like this one:

  wxJSONValue v["key"] = 100;
  int i;
  if ( v["key"].IsInt() ) {
    i = v["key"].AsInt();
  }
  else {
    cout << "Error: value is not of the expected type";
  }

This release adds a new version of all overloaded AsXxxxxx() function which stores the value in the provided argument and returns TRUE if the value stored in the JSON value object is of the correct type. This is the function prototype for integer value:

  bool AsInt( int& i );

Now you can get the value and check if it is of the expected type in only one call:

  wxJSONValue v["key"] = 100;
  int i;
  if ( !v["key"].AsInt( i ) ) {
    cout << "Error: value is not of the expected type";
  }

The new reader and writer organizaion

Until version 1.0 the wxJSON reader and writer had some issues mostly related to speed. The problem was that both the reader and the writer performed a character conversion from / to UTF-8 and unicode for every char read from / written to streams. Worst, in ANSI builds, every char undergoes to a double conversion for both the reader and the writer (the following is for the reader):

Also note that such a conversion is, for most characters, not needed at all because those chars are in the US-ASCII charset (0x00..0x7F).

In version 3.0 of the GUI framework, developers have introduced a radical change to Unicode support and the wxString class has totally changed in its internal organisation. In particular, the wxString class now stores strings in UTF-16 encoding on Windows and in UTF-8 on unix systems. The drawback is that on *nix systems the usual character access using subscripts such as:

    wxString s;
    s[n];

is VERY inefficient because of the UTF-8 encoding. The conseguence is that in wxJSON there is a speed issue also when the JSON text input is from wxString and not only from streams.

What are the goals of the new 1.1 version

In order to find the best organization for the reader and the writer I have to first point out what are the goals of this new release of wxJSON:

The new wxJSON organisation

The wxJSON library allows you to write / read JSON text to / from two different types of objects:

These two kinds of I/O classes are very different because of the internal representation of the JSON text: in particular, wxString uses UTF-16 on windows and UTF-32 on *nix systems up to wxWidgets 2.8. UTF-8 is used on *nix systems in wxWidgets 2.9. For streams the encoding is alwasy UTF-8. A further different encoding is used in ANSI mode: locale dependent one-byte characters.

Encoding formats in the different wxWidget's modes / versions /platforms
ver11.gif

These encoding differences complicates very much the organization of the writer and the reader because character read from / written to JSON text has to be converted to a unique type for processing. Actually, each char is converted to a wchar_t type and it occurs in ANSI mode, too. This conversion slows down the processing very much. A further complication is that wxWidgets 2.9 does no more return a char or wchar_t type when accessing string objects but a helper class: wxUniChar which has its own encoding format so that it has to be further converted to wchar_t.

The solution is to use only one encoding format for all types of I/O, build mode and wxWidget's versions: UTF-8 is the only one applicable to all these cases. Using UTF-8 as the unique I/O format has several advantages:

The only drawback is when input / output is not from / to a stream (which is in UTF-8 format) but from / to a wxString object. The solution I found is:

So, as opposed to the previous versions, the read / write operations are faster on streams and slower on strings because of the construction of the temporary UTF-8 memory buffers.

New feature in ANSI mode

The default behaviour of the wxJSON library in ANSI builds when reading from / writing to streams is to use UTF-8 encoded text so that the writer produces valid JSON text and the reader gives some limited Unicode support (see Unicode support: ANSI builds for details).

In version 1.1 I introduced a new feature for ANSI builds: both the reader and the writer can be constructed with a special flag:

  wxJSONREADER_NOUTF8_STREAM
  wxJSONWRITER_NOUTF8_STREAM

that suppress UTF-8 conversion:

The advantage of this ANSI mode is speed: string's data is simply copied from the stream to wxString by the reader; JSON values containing string are simply copied from the wxString object to the stream by the writer.

Also note that this feature can be used also if the stream is in UTF-8 format. By suppressing UTF-8 conversion the reader copies all UTF-8 code units in the wxString object: naturally, the meaning of those code-units are misunderstood by the wxString object which treats them as locale dependent one-byte characters: the advantage is that writing them back in ANSI mode just preserve the UTF-8 encoding because each UTF-8 code-unit is simply copied to the stream. The drawback is that the UTF-8 stream read by the wxJSON reader may contain characters that can be represented in the current locale. If UTF-8 conversion is disabled in the reader, no conversion takes place.

For example: suppose that in a UTF-8 input stream there is the following string:

  C3 A0 C3 A8 C3 AC C2 A9 C2 AE

which has to following meaning (five characters, ten UTF-8 code-units)

  àèì©®

The character's meaning could be correcly interpreted in an ANSI application localized in a Latin-1 charset If UTF-8 conversion is not disabled, the wxJSON reader reads the five Latin-1 characters correctly. On the other hand, if UTF-8 conversion is disabled, the ten UTF-8 code-units are simply copied to the wxString object which contains 10 characters whose meaning (in ths ISO-8859-1 charset) is the following:

  à èì©®

Generated on Fri Nov 13 22:52:30 2009 for wxJSON by  doxygen 1.5.5