Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I'm porting over a rest client from .NET to Apex and in the class that gets initiated by deserializing the JSON response, there is a byte[].

C# Example:

 public class MyResponse
  {
    public byte[] Image { get; set; }
  }

Wondering how to best handle this in apex? Maybe change it to a Blob or just a String type would work?

I likely don't really care about the data in the byte[], I just don't want it to break the Deserialization.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Assuming the system that is creating the JSON is using base64 encoding for the image, yes use Blob.

Here is an example of the deserialization/serialization working with base64 in Apex:

@isTest
private class ImageTest {
    private class MyResponse {
        Blob Image;
    }
    @isTest
    static void test() {
        // 2x2 PNG image
        String s = '{"Image":"iVBORw0KGgoAAAANSUhEUgAAAAIAAAACAgMAAAAP2OW3AAAADFBMVEWxf2/KgHhvwEfedoetG6yMAAAADElEQVQI12NoYCgAAAH0APFbzVilAAAAAElFTkSuQmCC"}';
        MyResponse b = (MyResponse) JSON.deserialize(s, MyResponse.class);
        String ss = JSON.serialize(b);
        System.assertEquals(s, ss);
    }
}
share|improve this answer
    
+1 for proving it with a test. Hopefully thats how they are encoding it... Unfortunately, I haven't gotten to see an actual response yet. –  NSjonas May 23 '14 at 16:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.