Skip to content
This repository has been archived by the owner on Dec 7, 2019. It is now read-only.

Parser from disk but not from fetcher #98

Closed
Exerosis opened this issue Jan 31, 2017 · 3 comments
Closed

Parser from disk but not from fetcher #98

Exerosis opened this issue Jan 31, 2017 · 3 comments

Comments

@Exerosis
Copy link

It would be very useful if one could get results from an API: eg. Stock stock = YahooFinance.get("NVDA"); Then persist that to disk using SourcePersisterFactory as a Json, then parse the result as it comes back from the disk.

@digitalbuddha
Copy link
Contributor

Currently the file system impelentation needs a stream from network. As a result SourcePersisterFactory cannot work with an input of a scalar value like Stock. What you can do is write your own persister which can still use the FileSystem, within your read/write impelentation you can do the conversion from your data type to a BufferedSource. Use the SourcePersisterFactory as an example (there should be a test for it which shows how to create a bufferedsource from a string)

You're also free to create a new middleware for this and we will merge it in. ☺️

Alternatively you can use your own persister without our file system and write a persister implementation that can work with an input/output of Stock.

@pavlospt
Copy link
Contributor

pavlospt commented Jan 31, 2017

Just a small note here, if you are using GSON you can very easily convert your Stock object to a BufferedSource.

Buffer buffer = new Buffer();
Gson gson = new Gson();
buffer.write(gson.toJson(yourObjectGoesHere).getBytes());
buffer.flush();
buffer.close();

@digitalbuddha
Copy link
Contributor

one more helper method how to go from a string to a bufferedsource

private static BufferedSource source(String data) {
        return Okio.buffer(Okio.source(new ByteArrayInputStream(data.getBytes(UTF_8))));
    }

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants