JSONGenerator gen = JSON.createGenerator(true); gen.writeStartObject(); gen.writeNumberField('abc', 1.21); gen.writeStringField('def', 'xyz'); String pretty = gen.getAsString();//This statement use at the end only .It ends the JSON System.debug('STRING JSON'+pretty); gen.writeStartObject(); gen.writeStringField('def', 'xyz'); gen.writeEndObject();
After the line 6 (where i am printing the log )i have the following statement in my debug log 13:24:03:048 FATAL_ERROR System.UnexpectedException: Salesforce System Error: 1148877694-2467 (681892207) (681892207)
I decided to read the document again and discovered that the when i use the method getAsString method of JSON generator class the apex automatically closes the JSON and hence attempting to start writing the Object after closing generates an error .
Though this is simple issue but the error message is not caught properly in the salesforce backend .
So the conclusion is always using getAsString of JSON generator class should be practiced .
Never use it even for the debug statements.
This is one of the rare cases where error message is misleading.Hope this helps for people trying to google and find why the error is occuring when using JSON generator.
Happy Coding !
Thanks for sharing, nice post! Post really provice useful information!
ReplyDeleteGiaonhan247 chuyên dịch vụ vận chuyển hàng đi mỹ cũng như dịch vụ ship hàng mỹ từ dịch vụ nhận mua hộ hàng mỹ từ website nổi tiếng Mỹ là mua hàng amazon về VN uy tín, giá rẻ.