JSONUtil Versions Save

JSON generation and parsing utility library for Java.

JSONUtil-1.10.4

7 years ago

The main change in this release is that the default for undefined code points is now JSONConfig.PASS. I was confused by a document that was primarily concerned with display. This package isn't really doing display so replace isn't really an appropriate default. Unmatched surrogates remain as default to be replaced. Replace is still available as an option. It just isn't the default for undefined code points anymore.

There is also a small optimization for the toJSON methods that return a string. It probably won't matter unless you use those a lot and have a lot of strings that need escaping.

If you're using Java 7 or earlier, download the jars with your Java version number. Otherwise just get the regular jars that don't have a Java version in their name. They require a minimum of Java 8.

The binaries will also be available at the Maven Central Repository shortly.

JSONUtil-1.10.2

7 years ago

This release changes the way that bad character data is handled. There are two types of bad character data. One is unmatched surrogates. The other is code points that are undefined by the Unicode standard (which JSONUtil uses the JDK to recognize). Previously, these were both escaped. It turns out that was the wrong behavior. The correct behavior is to replace such characters or code points with the Unicode replacement character U+FFFD. I apologize for changing default behavior but when default behavior is wrong, it's a bug. I have created options (described below) for those that want the old behavior back.

In reality, most applications don't encounter bad character data so this probably isn't critical but I want it to be correct and I also want flexibility so JSONUtil now offers a choice of 5 different behaviors for bad character data. You can select these behaviors via JSONConfig/JSONConfigDefaults using the methods setUnmatchedSurrogatePolicy(int) and setUndefinedCodePointPolicy(int). There is also a setBadCharacterPolicy(int) method which just calls both of those two with the same value. These policies can also be set in JNDI and read/modified by the JMX Mbean

The 5 behaviors are: 0. JSONConfig.REPLACE (default) - replace the bad character(s) with the Unicode replacement character U+FFFD

  1. JSONConfig.DISCARD - discard bad characters.
  2. JSONConfig.EXCEPTION - throw an exception when bad character data is encountered.
  3. JSONConfig.ESCAPE - escape bad characters
  4. JSONConfig.PASS - pass bad characters through as if they are OK

If you're using Java 7 or earlier, download the jars with your Java version number. Otherwise just get the regular jars that don't have a Java version in their name. They require a minimum of Java 8.

The binaries will also be available at the Maven Central Repository shortly.

JSONUtil-1.10.1

7 years ago

1.10.0 improved performance for string values that did not contain code points that needed to be escaped. This release improves performance for strings that do need escaping. If your strings need a lot of escaping then it dramatically improves performance. Generation of a single escape is roughly 30-50 times faster than before due to using a custom escape generator.

JSONUtil now provides a boolean property name validator if you want to validate property names.

JsonObject has been enhanced to take a reference to a JSONConfig object which can be passed to the constructor or to a setter. When its toString or toJSON methods that don't take a JSONConfig as a parameter are called, it will use that as its configuration object. If you pass a null config to the other ones, it will also use the one it has stored.

All of the public methods in JSONConfig which had a void return type now return the JSONConfig object itself. This is so that you can call such methods in series as with JsonObject.add().

If you're using Java 7 or earlier, download the jars with your Java version number. Otherwise just get the regular jars that don't have a Java version in their name. They require a minimum of Java 8.

The binaries are also available at the Maven Central Repository

JSONUtil-1.10.0

7 years ago

Strings which do not contain characters which need to be escaped are now encoded dramatically faster. In my tests with large strings (1024 chars) encoding times were reduced by as much as 70-80%. This was accomplished by putting a check at the start to see if there are any characters which need to be escaped and if not then just write out the whole string in one shot instead of iterating over the code points.

If the new string encoding still isn't fast enough and you know for a fact that your strings do not have any characters which need to be escaped, you can use the new JSONConfig.setFastStrings(true) option (also available with JSONConfigDefaults). This eliminates all checking for escapes and writes the strings out with quotes around them. It's very fast this way but if you have characters that need to be escaped, it could break your JSON. Use with care.

When iterating over the code points in strings, handling of bad surrogate pairs has been made more sane and reasonable, though I'm not sure what the correct behavior for bad surrogate pairs really should be. For now, bad surrogates are escaped but kept.

Version 1.9 introduced the "preciseNumbers" option. Unfortunately, the code to handle it slowed down all numbers regardless of whether the option was set or not. That has been fixed and number performance has been further optimized and is now very fast, especially if you don't use custom number formats or BigDecimal or BigInteger.

There is now a JsonObject convenience class. You can use this in place of maps by adding key-value pairs with its add(Object,Object) method. The add method returns the JsonObject itself so that you can do adds in series like this:

JsonObject myObj = new JSONObject().add("x",3).add("y","some string").add("z",3.14);

JsonObject implements JSONAble so it has its four toJSON() methods available for convenience and it is recognized as such when it is encountered inside other data structures.

If you're using Java 7 or earlier, download the jars with your Java version number. Otherwise just get the regular jars that don't have a Java version in their name. They require a minimum of Java 8.

The binaries are also available at the Maven Central Repository

JSONUtil-1.9.3

7 years ago

This release is primarily driven by optimizations to the reflection code. Both cached and uncached reflection performance has been significantly improved. Reflection performance is now quite good.

If you're using Java 7 or earlier, download the jars with your Java version number. Otherwise just get the regular jars that don't have a Java version in their name. They require a minimum of Java 8.

The binaries are also available at the Maven Central Repository

JSONUtil-1.9.2

7 years ago

This is another minor release to fix some minor bugs that nobody was likely to notice, improve reflection performance and add some minor features.

For JMX MBean users, the Locale setting now works properly and has been renamed LocaleLanguageTag. This allows you to view and modify the Locale via a IETF BCP 47 language tag. The set worked properly before but the get did not. It does now.

Reflection users can now change the names of fields when they are encoded in JSON. For example if a field is named "foo" in the object but you want it called "bar" in the JSON output, you can create an alias for it by using selective reflection with a JSONReflectedClass object and setting a fieldAliases Map in the constructor or via a setter to map the names.

Performance has been improved for creating new JSONConfig objects when selective reflection classes are set by default as well as when cloning JSONConfig objects.

The addReflectClassByName(String) which was previously only available in JSONConfigDefaults is now also available in regular JSONConfig as a convenience method since you can use it to add field selection or field aliases in one string without creating a separate collection or alias map by hand.

If you're using Java 7 or earlier, download the jars with your Java version number. Otherwise just get the regular jars that don't have a Java version in their name. They require a minimum of Java 8.

The binaries are also available at the Maven Central Repository

JSONUtil-1.9.1

7 years ago

There were a couple of bugs that couldn't really wait for the next major release so this is a minor one.

The reflection code from 1.9 did not recognize boolean getters that started with "is". That has been fixed.

There was a problem with the resource name tags for a class loading error. That has been fixed.

That said, there are some small new features:

The reflection code has been further optimized. It's still quite a bit slower than making maps manually or using reflection with cached data but it's significantly faster than it was in 1.9.

For JNDI startup, there is no longer a limit on the number of date parsing formats you can include. They simply have to have names that follow the pattern "dateParseFormatD" where "D" is some non-negative decimal integer. The formats will be loaded and tried in numerical order of their JNDI names.

JNDI reflection class loading no longer needs or uses an index limit to determine the names to look for. It tries to load any class that has a JNDI name following the pattern a"reflectClassD" where D is a non-negative decimal integer. The order of numbers is ignored in this case because loading order is irrelevant. It only matters that the names be unique because they are used as keys in a Map.

If you're using Java 7 or earlier, download the jars with your Java version number. Otherwise just get the regular jars that don't have a Java version in their name. They require a minimum of Java 8.

The binaries are also available at the Maven Central Repository

JSONUtil-1.9

7 years ago

This release includes several new features.

The most notable is that reflection of objects for encoding JSON is now supported. There is a Wiki page about how to use reflection with this package. It can reflect only certain classes that you choose or you can have it reflect all unknown objects. You can selectively pick fields to include with objects or you can let it pick based upon class privacy (public, protected, package or private). There is an option for caching to help with reflection performance at a cost of memory.

Using Reflection to Encode Objects as JSON

JSONParser now supports a JSONConfig option called "smallNumbers" which tries to store numbers in the smallest primitive possible without losing information.

It also supports an option called "usePrimitiveArrays" that causes it to look for lists that look like arrays of primitives and if possible, turn them into arrays of primitives using the smallest primitive type possible without losing information.

When the parser cannot store a number in a 64-bit double or a 64-bit long integer without losing precision, then it will use BigDecimal or BigInteger to store the number. You can decide if you want to lose precision after that.

If the JSONConfig option encodeNumericStringsAsNumbers is true, then the parser will look at strings to see if they look like numbers and will encode them as such if they do.

There is another new option called "preciseNumbers". When it is true, then any time that a number that is being encoded cannot be accurately represented in 64-bit double precision floating point (the only numbers in Javascript) then the number string will be quoted instead of bare as numbers normally are. This gives the option of using those strings with arbitrary precision arithmetic packages (there are several available for Javascript).

JNDI now supports setting a string called org/kopitubruk/util/json/appName which is used to augment the JMX MBean name so that multiple apps can have their own version of the JSONConfigDefaults MBean in the same web tier container at the same time. It can also be set with a -D option on the command line (using dots instead of slashes) if you're not using JNDI.

There was a bug with indent padding and null values. It has been fixed.

If you're using Java 7 or earlier, download the jars with your Java version number. Otherwise just get the regular jars that don't have a Java version in their name. They require a minimum of Java 8.

The binaries are also available at the Maven Central Repository

JSONUtil-1.8

7 years ago

This release fixes a few bugs and adds a new config flag.

It turns out that passing escapes through in strings is a bad idea, especially when code is passing Windows path names that have backslashes in them as "C:\not\right" gets interpreted as having a newline and a return in it. The default behavior is now to escape all inline backslashes rather than pass them through. You can still get the old behavior by using JSONConfig.setPassThroughEscapes(boolean). That method is also available in JSONConfigDefaults as well as through JNDI and MBean access.

There was a bug in formatting the error messages for unrecognized data. It has been fixed.

There was a white space bug in the JSONParser class that has been fixed.

If you're using Java 7 or earlier, download the jars with your Java version number. Otherwise just get the regular jars that don't have a Java version in their name. They require a minimum of Java 8.

The binaries are also available at the Maven Central Repository

JSONUtil-1.7.1

7 years ago

This release fixes a minor bug in the IndentPadding that sometimes caused the wrong indent level.

For those that are interested in the source, there has been more code reorganization. appendRecursivePropertyValue() had grown too large and its code has been broken up into other methods and some of it in other classes.

If you're using Java 7 or earlier, download the jars with your Java version number. Otherwise just get the regular jars that don't have a Java version in their name. They require a minimum of Java 8.

The binaries are also available at the Maven Central Repository