1

How is a JSONObject from Twitter4J converted to an IOFile for use with the JsonParser from BaseX?

So, looking to use Twitter4J to get either a file or stream.

The JsonParser looks to work with a File fine:

JsonParser

public JsonParser(IO source,
          MainOptions opts,
          JsonParserOptions jopts)
           throws java.io.IOException

Constructor.

Parameters:
    source - document source
    opts - database options
    jopts - parser options
Throws:
    java.io.IOException - I/O exception

although other IO works:

org.basex.io
Class IO

    java.lang.Object
        org.basex.io.IO 

    Direct Known Subclasses:
        IOContent, IOFile, IOStream, IOUrl 

How is a File acquired from a JSONObject here?

Snippet using Twitter4J:

private JSONObject jsonOps(Status status) throws JSONException, BaseXException {
    String string = TwitterObjectFactory.getRawJSON(status);
    JSONObject json = new JSONObject(string);
    String language = json.getString("lang");
    log.fine(language);
    return json;
}
Thufir
  • 8,216
  • 28
  • 125
  • 273
  • What makes you think a File is, or can be, “acquired”? And why do you want a File when working with a Twitter feed? – Bohemian Feb 01 '20 at 08:43
  • I'm trying to convert to String, then writing to a File for BaseX, which requires a File. – Thufir Feb 01 '20 at 08:54
  • 1
    How about converting the object to a string and writing that string to a file? – Bohemian Feb 01 '20 at 09:09
  • yes @Bohemian that's what I've done just now. and then I'm now working on loading the file from `JsonParser` -- but that just seems silly from the outset. I just want to send "json" to the `JsonParser`. – Thufir Feb 01 '20 at 09:24
  • 1
    Use a stream? A `ByteArrayInputStream` from the String specifically. – Kayaman Feb 01 '20 at 10:31
  • I'm not sure I asked the question properly, @Kayaman, but yes, I used a stream as in the answer. – Thufir Feb 01 '20 at 10:42
  • 1
    @Thufir yes, but I meant without writing to a `File` first. I'm pretty sure `JSonParser` can read from an `InputStream`, and you can get a `ByteArrayInputStream` from `String.getBytes(charset)`. Or a `Reader` and a `StringReader`. Ah, Basic IO, why have you been forgotten. – Kayaman Feb 01 '20 at 10:46
  • 1
    It's that BaseX library that's throwing things off, but no, you do not need to store things to a file and read them back that you already have in the memory. – Kayaman Feb 01 '20 at 10:51
  • Yeah, my "solution" is more than a bit smelly -- but works so far as I can tell, in that the parser is instantiated. I'll come back to this in a bit :) – Thufir Feb 01 '20 at 10:54

1 Answers1

0

Writing a JSONObject to file file from Twitter4J seems to work, at least the file looks correct when opened manually:

public void writeJsonToFile(String fileName) throws FileNotFoundException, UnsupportedEncodingException, IOException {
    FileOutputStream fileOutputStream = null;
    OutputStreamWriter outputStreamWriter = null;
    BufferedWriter bufferedWriter = null;
    fileOutputStream = new FileOutputStream(fileName);
    outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
    bufferedWriter = new BufferedWriter(outputStreamWriter);
    bufferedWriter.write(tweets.toString());
    bufferedWriter.close();
    outputStreamWriter.close();
    fileOutputStream.close();
}

And once the file is written, it seems quite easy to "parse" the JSON, or at least instantiate a parser, as:

private void parseJsonFile(String fileName) throws IOException {
    JsonParser jsonParser = new JsonParser(new IOFile(fileName), new MainOptions());
}

full code on github.

In no way is this a complete solution. In fact, it seems a kludge to write the JSON to a file at all -- but there it is.

Seems the only way to move JSON data from Twitter4J to BaseX, or at least most pragmatic. Other solutions appreciated.

Thufir
  • 8,216
  • 28
  • 125
  • 273