Fix NPE in toJSON method