Presentation is loading. Please wait.

Presentation is loading. Please wait.

Handling JSON in Apex Shamil Arsunukayev Technical Architect, Comity Designs

Similar presentations


Presentation on theme: "Handling JSON in Apex Shamil Arsunukayev Technical Architect, Comity Designs"— Presentation transcript:

1 Handling JSON in Apex Shamil Arsunukayev Technical Architect, Comity Designs Inc. @shamiltech

2 Agenda  System.JSON  System.JSONGenerator  System.JSONParser  Demo: Google Calendar APIs + JSON + Apex  Q&A

3 System.JSON JSON.serialize: Account acctOne = [SELECT id, name FROM Account LIMIT 1]; String acctJSON = JSON.serialize(acctOne); JSON.deserialize: Account acctTwo = (Account) JSON.deserialize(acctJSON, Account.class);  Minimum number of script statements  No need to use Dynamic Apex to inspect the metadata, it’s all automatic!

4 CalendarList Resource JSON { "kind": "calendar#calendarListEntry", "etag": etag, "id": string, "summary": string, "hidden": boolean, … "defaultReminders": [ { "method": string, "minutes": integer } ] }

5 GoogleCalendar Apex Class public class GoogleCalendar { public String id; public String kind; public String etag; public String summary; public Boolean hidden; … public List defaultReminders; }

6 System.JSON public String serialize() { return JSON.serialize(this); } public void deserialize(String jsonString) { GoogleCalendar gCal = (GoogleCalendar) JSON.deserialize(jsonString, GoogleCalendar.class); … }  Notice how Apex objects are used instead of sObjects

7 System.JSON Note: If GoogleCalendar Apex class didn’t have a field or fields to match the CalendarList JSON structure, the JSON.deserialize() method would have failed with the “System.JSONException: Unknown field field_name “ exception

8 JSONGenerator  Contains methods used to serialize Apex objects into JSON content using the standard JSON encoding.  Useful for wrapping or aggregating values  Can be used instead of the JSON.serialize() if the latter could not be used List contacts = [Select Id, Name From Contact]; generator.writeStartObject(); generator.writeNumberField(‘count’, contacts.size()); generator.writeEndObject(); String jsonString = generator.getAsString();

9 Event Resource JSON { “summary": string, “end": { <-------- ‘end’ is a reserved Apex keyword "date": date, <-------- ‘date’ is a reserved Apex keyword … }, "attendees": [ { "email": string, … } ], "reminders": { "useDefault": boolean, "overrides": [ { "method": string, "minutes": integer } ] }

10 GoogleCalendarEvent Apex Class public class GoogleCalendarEvent { public String summary; … public GoogleEventTime start; public GoogleEventTime gEnd; //renamed ‘end’ public List attendees; public GoogleReminder reminders; }

11 JSONGenerator JSONGenerator gen = JSON.createGenerator(true); gen.writeStartObject(); gen.writeStringField('summary', ‘New event’); gen.writeFieldName('start'); gen.writeStartObject(); gen.writeObjectField('dateTime', System.now().addDays(1)); //or gen. writeDateTimeField('dateTime', System.now().addDays(1)); gen.writeEndObject(); { “summary”: “New event", "start": { "dateTime": "2012-03-16T18:03:32-08:00“ }, …

12 JSONGenerator gen.writeFieldName('reminders'); gen.writeObject(reminders); //reminders is a GoogleReminder object … "reminders": { "useDefault": false, "overrides": [ { "method": "email", "minutes": 1 }, { "method": "email", "minutes": 2 } ] }…

13 GoogleReminder Apex Class public class GoogleReminder { public Boolean useDefault; public List overrides; } public class GoogleReminderOverride { public String method; public Integer minutes; }

14 JSONGenerator gen.writeFieldName('attendees'); gen.writeStartArray(); for(GoogleEventAttendee gEventAttendee: this.attendees){ gen.writeStartObject(); gen.writeStringField('email', gEventAttendee.email); gen.writeBooleanField('optional', gEventAttendee.optional); gen.writeNumberField('additionalGuests', gEventAttendee.additionalGuests); gen.writeEndObject(); } gen.writeEndArray(); … String jsonString = gen.getAsString(); //create a JSON string

15 JSONGenerator "attendees": [ { "email": "attendeeOne@gmail.com", "optional": false, "additionalGuests": 1 }, { "email": "attendeeTwo@gmail.com", "optional": true, "additionalGuests": 2 },... ]

16 JSONParser  Generally, JSONParser is useful for grabbing specific pieces of data without the need for a structure such as an Apex class JSONParser parser = JSON.createParser(resp); while (parser.nextToken() != null) { if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)){ String fieldName = parser.getText(); parser.nextToken(); if(fieldName == 'access_token') { accesstoken = parser.getText(); } else if(fieldName == 'expires_in'){ expiresIn = parser.getIntegerValue(); } }}

17 JSONParser "attendees": [ { "email": "attendeeOne@gmail.com", "optional": false, "additionalGuests": 1 }, { "email": "attendeeTwo@gmail.com", "optional": true, "additionalGuests": 2 },... ]

18 JSONParser … if(fieldName == 'attendees'){ if(parser.getCurrentToken() == JSONToken.START_ARRAY){ while(parser.nextToken() != null){ if(parser.getCurrentToken() == JSONToken.START_OBJECT){ GoogleEventAttendee gEventAttendee = (GoogleEventAttendee) parser.readValueAs(GoogleEventAttendee.class); this.attendees.add(gEventAttendee); } else if(parser.getCurrentToken() == JSONToken.END_ARRAY){ break; }}}}

19 Demo: Google Calendar APIs + JSON + Apex

20 Thank you Questions & Answers


Download ppt "Handling JSON in Apex Shamil Arsunukayev Technical Architect, Comity Designs"

Similar presentations


Ads by Google