简体   繁体   中英

How to store and load User with Twitter4j

In Twitter4j, you get a user object like following,

User user = twitter.showUser(userID);

Now, I want to store this user in a text file. I'm using the toString() method of the User .

It gives me something like the following:

UserJSONImpl{id=xxxxxx, name='xxxx xxxxx', screenName='xxxxxx', ... }

Questions:

  1. Is using toString() method of User for storing purposes legitimate?

  2. How to load a user using this UserJSONImpl text? I need something like the following:

User user = new User(UserJSONImpl);

I think relying directly on toString() for this is a bad idea because changes to this method are out of your control. For example, what if the format changes or a new field is added? How do you know when you come to deserializing the data?

You could use a library to write the User objects in a standard format, for example use GSON to write JSON - but it's still not nice, you have to deal with UserJSONImpl objects which is an internal representation of User .

Either way, you could still be exposed to the risk of changes in the User object. I would consider using the User objects to build an object relevant the problem domain, and then persist these objects.

The idea is that the component that's responsible for the Twitter4J User -> problem domain object conversion can handle different Twitter4J User objects and provide a consistent view for your application. Although without more context this may be complete overkill, you should be able to judge this better. I hope this helps somewhat!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM