Jackson won't wrap class - java

I have a class that holds contact data; wrapped in a respective class. I recently changed my Photo setup from being a simple byte[] to being a wrapped class as well, but the instantitaion is a little different and now won't serialize/wrap properly.
My other classes wrap properly such as "number":{"log.PhoneNumber":{"number":"123-456-7890"}} but if I feed in a new photo (ie: new Photo("DEADBEEF")) I just get "photo":"DEADBEEF". This is causing problems with the deserializer too.
public class ContactInfo {
#JsonProperty("name") private Name m_name = null;
#JsonProperty("number") private PhoneNumber m_number = null;
#JsonProperty("email") private Email m_email = null;
#JsonProperty("photo") private Photo m_photo = null;
#JsonCreator
public ContactInfo(#JsonProperty("name") Name name,
#JsonProperty("number") PhoneNumber number,
#JsonProperty("email") Email email,
#JsonProperty("photo") Photo photo) {
/** Set vars **/
}
#JsonTypeInfo(use=Id.CLASS, include=As.WRAPPER_OBJECT)
static public class Photo {
private byte[] m_decodedBase64 = null;
public Photo(byte[] encodedBase64) {
m_decodedBase64 = Base64.decodeBase64(encodedBase64);
}
#JsonCreator
public Photo(#JsonProperty("photoData")String encodedBase64) {
m_decodedBase64 = Base64.decodeBase64(encodedBase64);
}
#JsonProperty("photoData")
public String getEncodedPhoto() {
return Base64.encodeBase64String(m_decodedBase64);
}
public byte[] getDecodedData() {
return m_decodedBase64;
}
}
}
What am I doing wrong?

Just figured out what it was. In the ContactInfo class there was a simple accessor function to get the encodedData.
public String getPhoto() {
return m_photo.getEncodedPhoto();
}
By simple putting it on ignore (or simply change it to return the object itself, which I might do),
#JsonIgnore
public String getPhoto() {
return m_photo.getEncodedPhoto();
}
The serializer stopped trying to read from it. I wish there was a way to set the serializer engine to be more "explicit declaration" for properties instead of "serialize everything that seems to match the member variables."

Related

How to implement read and write methods of Chronicle Map SizedWriter and SizedReader interfaces for class with string member

I have created a simple class:
public class Example
{
private String name;
private int age;
// With getters and setters.
}
that I would like "put" into a chronicle map:
ChronicleMap<String,Example> map = ChronicleMapBuilder
.of(String.class, Example.class)
.name("example-map")
.entries(5_000)
.averageValue(new Example())
.valueMarshaller(ExampleSerializer.getInstance())
.averageKey("Horatio")
.createPersistedTo(new File("../logs/example.txt"));
However, I do not fully understand how to implement the ExampleSerializer class because I am not sure how the string member variables should be handled. How do I size strings? In the read and write methods, how do I read the string member variable, and how do I write the string member variable respectively. Pls note that on average, the name member string length will be between 7-10 characters. I have created the serializer below:
public class ExampleSerializer implements SizedReader<Example>,SizedWriter<Example>
{
private static ExampleSerializer INSTANCE = new ExampleSerializer();
public static ExampleSerializer getInstance() { return INSTANCE; }
private ExampleSerializer() {}
#NotNull
#Override
public Example read(Bytes in, long size, #Nullable Example using)
{
if (using == null)
using = new Example();
using.setAge(in.readInt());
using.setName(in.readUtf8()); // NOT SURE IF THIS IS CORRECT FOR A STRING
return using;
}
#Override
public long size(#NotNull Example toWrite)
{
return Integer.BYTES + ???; // NOT SURE WHAT THE SIZE SHOULD BE FOR STRING MEMBER?
}
#Override
public void write(Bytes out, long size, #NotNull Example toWrite)
{
out.writeInt(toWrite.getAge());
out.writeUtf8(toWrite.getName()); // NOT SURE IF THIS IS CORRECT FOR A STRING
}
}

Android: get local resources in enumerate class

I'm writing an android application with java code. The app can support english and italian. Inside the app, there is a spinner that take its values from an enumerate class, the following:
public enum ElementTypesEnum {
MEET("Meet"),
CEREAL("Cereal"),
FISH("Fish"),
OTHER("Other");
private String elementType;
ElementTypesEnum(String elementType) {
this.elementType = elementType;
}
public String getElementType() {
return elementType;
}
}
I want to initialize the values of the enumerate with the values contained in my local string resource file (R.string.value_1). In this class I don't have an instance of the resource file, since I don't have an instance of Context. How can I do this? Thank you in advance, Marco
Use the resource ID then fetch the strings with your spinner's Context when you populate it.
public enum ElementTypesEnum {
MEAT(R.string.meat),
CEREAL(R.string.cereal),
FISH(R.string.fish),
OTHER(R.string.other);
#StringRes
private int elementType;
ElementTypesEnum(#StringRes int elementType) {
this.elementType = elementType;
}
public int getElementType() {
return elementType;
}
}

how to change name based on activity in model using android

I create a model called Review. There are two activities (QualityReivewActivity.java & FairnessReivewActivity.java) will call the model whenever user leaves a comment.
I want to
public class Review {
float fairness_rating; //change the name to quality_rating
String post_id;
String review_time;
String reviewer_id;
String text_review;
public Review(){
//
}
Review(float fairness_rating, String post_id, String review_time, String reviewer_id, String text_review){
this.fairness_rating=fairness_rating;
this.post_id=post_id;
this.review_time=review_time;
this.reviewer_id=reviewer_id;
this.text_review=text_review;
}
public float getFairness_rating() {
return fairness_rating;
} //change to getQuality_rating if actitivty is QualityReivewActivity.java
public String getPost_id() {
return post_id;
}
public String getReview_time() {
return review_time;
}
public String getReviewer_id() {
return reviewer_id;
}
public String getText_review() {
return text_review;
}
}
and this is the segment of code of QualityReivewActivity
Review c = new Review(mRatingBar.getRating(), post_id, timedComment.toString(), reviewer_uid, my_comment.getText().toString()); //
However, the QualityReviewActivity always shows "fairness_rating". How can I make a dynamic model name to change to "quality_rating" if I am calling from QualityReviewActivity?
You can solve it by using enum.
public enum ratingType {
 Quality,
 Fairness
}
Then use that enum in your function.
public float get_rating(ratingType type) {
 switch (type) {
 case Quality:
  return quality_rating;
 case Fairness:
  return fairness_rating;
 }
}
After that, depending on the class, you can pass the necessary arguments and it will work.
I'm not good at Java, so it may not work if I write it as it is, but the idea is ok.

How to get the data that is stored in ArrayList from another class in Java/Android? [duplicate]

This question already has answers here:
How to access ArrayList from another class in Android Java?
(3 answers)
Closed 7 years ago.
I'm new in Java/Android and I'm trying to do one thing, but I'm not sure if I can or I can't do it.
My problem is this: I'm parsing a Json and I send this json to my class. All is correct, json works and the data is stored correctly. That I want to do, is access to the data that I've stored in the arrayList from another class, but I don't know how to do it.
I've tried to implement a singleton java class, but I can't access to the data.
That I said is for exampl. If I create this method I can access to the data, but I need to pass the data from my json to the method.
public String showOverlay(ArrayList<ScreenEvent> config){
String show = "";
String empty = "empty";
for(ScreenEvent client : config){
show = client.action;
if(show.equals("show"))
return show;
}
return empty;
}
I don't want to do this. I want to be able to create an object of the arrayList inside of my method:
public String myMethod(){
//I want access here to the data of the arrayList
return empty;
}
I read a json and pass the data in a ArrayList:
public static ArrayList<VsClientConfig.ScreenEvent> eventConfig = new ArrayList<VsClientConfig.ScreenEvent>();
//JSON stuff
VsClientConfig.ScreenEvent vs = VsClientConfig.ScreenEvent.getScreenEvent(action, className, typeEvent, viewId, colourEvent);
eventConfig.add(vs);
This is my class:
public class VsClientConfig{
public String colour;
public String height;
public static class ScreenEvent {
public String action;
public String className;
public String typeEvent;
public String viewId;
public String colourEvent;
private static ScreenEvent miScreenEvent;
public static ScreenEvent getScreenEvent(String action, String className, String typeEvent, String viewId, String colourEvent) {
if (miScreenEvent == null) {
miScreenEvent = new ScreenEvent(action, className, typeEvent, viewId, colourEvent);
}
return miScreenEvent;
}
private ScreenEvent(String action, String className, String typeEvent, String viewId, String colourEvent) {
this.action = action;
this.className = className;
this.typeEvent = typeEvent;
this.viewId = viewId;
this.colourEvent = colourEvent;
}
}
public String myMethod(){
//I want access here to the data of the arrayList
return empty;
}
...
Create and initialize static arrayList in a Common class like below:
public class Common{
public static ArrayList<VsClientConfig.ScreenEvent> eventConfig=new ArrayList<>();
}
And assign if from wherever you want like:
//JSON stuff
VsClientConfig.ScreenEvent vs = VsClientConfig.ScreenEvent.getScreenEvent(action, className, typeEvent, viewId, colourEvent);
Common.eventConfig.add(vs);
Now Common.eventConfig (your arrayList) will be accessible to through your application

Android: Class problem

I've created a class called website, and want to access it like a variable so I can update values to it, probably better explained below:
Website w = new Website();
w.URL="stackoverflow.com";
Here's the code for the class:
class Website {
public String URL;
public Website(){
URL = "";
}
}
I would also like to add a method such as this:
public long save() {
return db.save(URL);
}
This (the method) isn't working for me at the moment
I would do it more OO way, hiding this URL variable from outside and letting change it's value from getter and setter methods. You can try this, maybe this will help.
In Website class
public class Website {
private String URL;
public Website(){
this.URL = "";
}
public void setUrl(String url) {
this.URL = url;
}
public String getUrl() {
return this.URL;
}
public long save() {
return db.save(this.URL);
}
}
And then call it
Website w = new Website();
w.setUrl("http://www.stackoverflow.com");
long someLongValue = w.save();

Categories