Skip to content

loadJSONOject() fails with compressed json sent by an API #710

@processing-bot

Description

@processing-bot

Created by: fabwoj

Description

I use an API which now send me compressed json.
loadJSONObject() fails to load this type of JSON.

Expected Behavior

allow loadJSONObject() to get compressed json

Current Behavior

When you use loadJSONObject() to load a compressed json file, the code crashes.

Steps to Reproduce

Unfortunately I can't give you a link to this private API which return compressed json.
You must find one. Sorry.

Your Environment

I use an old version of processing (2.2.1) for some reasons (need Unfolding library in this case).
I don't know if we have the same problem under the latest version of Processing.

  • Processing version: 2.2.1
  • Operating System and OS version: windows
  • Other information: compressed JSON provided by an API

Possible Causes / Solutions

I found a workaround which only works with compressed json. It seems to work. I don't really understand all the code lines inside.
Here, my code :

import java.net.*;
import java.io.*;
import java.util.zip.GZIPInputStream;

void setup(){
size(200,200);
JSONObject json=getData("http://bla-bla");  
JSONArray values = json.getJSONArray("something to get");
println(values.size()); 
noLoop();
}

JSONObject getData(String urlsite){
  StringList sb;
  JSONObject json = new JSONObject();
  try{
    URL url = new URL(urlsite);
    URLConnection conn = url.openConnection();  
    conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
    BufferedReader in = new BufferedReader(new InputStreamReader(new GZIPInputStream(conn.getInputStream()), "UTF-8"));
    String readLine;
    sb = new StringList();
    while ( (readLine = in.readLine ()) != null) {
      System.out.println(readLine);
      sb.append(readLine + '\n');
    }
    json = parseJSONObject(sb.get(0));
    in.close();
  }catch (MalformedURLException e) {
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  }
  return json;
}

I think it would be great if you can add an argument to loadJSONObject().
Something like loadJSONObject(filename / API URL, compressed json / not compressed json).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions