I used an ArrayList to store data on Firebase.It got stored as a HashMap with key value starting from 0 and its value stored as string.
The database is as follows:
I am using POJO, to store and retrieve data.
My POJO is defined as follow:
#JsonIgnoreProperties(ignoreUnknown = true)
public class POJO_MemoriesRVDisplay {
String mem_name;
String mem_place_street_address;
HashMap<Object,Object> images;
public POJO_MemoriesRVDisplay() {
}
public HashMap<Object, Object> getImages() {
return images;
}
public void setImages(HashMap<Object, Object> images) {
this.images = images;
}
public POJO_MemoriesRVDisplay(String mem_name, String mem_place_street_address, HashMap<Object, Object> images) {
this.mem_name = mem_name;
this.mem_place_street_address = mem_place_street_address;
this.images = images;
}
public String getMem_name() {
return mem_name;
}
public void setMem_name(String mem_name) {
this.mem_name = mem_name;
}
public String getMem_place_street_address() {
return mem_place_street_address;
}
public void setMem_place_street_address(String mem_place_street_address) {
this.mem_place_street_address = mem_place_street_address;
}
}
When I run this code, I get an error such as:
Failed to bounce to type
How do I declare the POJO properly. I have tried several posts but couldn't help it. Tried changing the declaration of images to strings, still wouldn't work. Please help!
The problem was in the Hashmap Declaration. Initially declaring it as
Hasmap<Object, Object> images;
didn't work in the POJO but after hours of trial and error I deleted the POJO and restarted the system. When I recreated the POJO file, I declared the Hashmap as:
Hashmap images;
Try with this solution:
//First create your object
POJO_MemoriesRVDisplay pojo = new POJO_MemoriesRVDisplay("School", "12345", Your hashmap);
//Create your reference where you want to store your data
private DatabaseReference root = FirebaseDatabase.getInstance().getReference();
//Save your data
root.push().setValue(pojo);
Related
I have a Switch that contains 13 case, each case executes a different sql request. I got the result in an ArrayList<HashMap<String, String>>. This result is supposed to be displayed with angular , for now i'm using this this.respTest = JSON.stringify(response); so it displays a list of "key":"value" .
My problem is since each request gets me different database fields and values ,so I want to merge some fields .
I created this class :
public class DataCollect {
private String type ;
private String entity ;
private String modPar ;
private String dateModif ;
private String numVersion ;
public DataCollect(String type, String entity, String modPar, String dateModif, String numVersion) {
this.type = type;
this.entity = entity;
this.modPar = modPar;
this.dateModif = dateModif;
this.numVersion = numVersion;
}
public DataCollect() {
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getEntity() {
return entity;
}
public void setEntity(String entity) {
this.entity = entity;
}
public String getModPar() {
return modPar;
}
public void setModPar(String modPar) {
this.modPar = modPar;
}
public String getDateModif() {
return dateModif;
}
public void setDateModif(String dateModif) {
this.dateModif = dateModif;
}
public String getNumVersion() {
return numVersion;
}
public void setNumVersion(String numVersion) {
this.numVersion = numVersion;
} }
In this class I'm supposed to affect the fields' names to the variables that I created and as a return an arraylist of hashmap with the data I extracted from the data base.
I mean I used to return for example "field-name":"value" , I want to return "type":"value","entity":"value" ..etc
I'm using springboot for the backend and angular 5 for the front.
Any help would be appreciated.
What you essentially want is a way to map keys in [each of] your hashmap to the corresponding member variable in the "DataCollect" POJO.
If there is a one to one mapping between the key present and corresponding member variable, you can expose a public constructor in "DataCollect" that takes in the hash map and constructs the corresponding object.
public DataCollect(Map<String, String> result) {
this.type = result.get("type");
this.entity = result.get("db_entity_key");
...
}
If there is no one on one mapping, you'd have to create a factory class, which takes your Map as an input and some context, and returns you the constructed DataCollect object.
Once you have the constructor or the factory class, you only need to iterate over your results list and do the needful to convert each Map into the DataCollect object.
Your controller should automatically serialise the DataCollect objects to corresponding JSON, or you can even use Jackson's ObjectMapper to achieve the same.
I want to create a ArrayList of data and return it via Rest point. I tried this:
#Service
public class CardBrandsListService {
public ArrayList<String> getCardBrandsList() {
ArrayList<String> list = new ArrayList<String>();
list.add("visa");
list.add("master");
list.add("Intl Maestro");
list.add("amex");
return list;
}
}
Rest endpoint:
#GetMapping("/card_brand/list")
public ResponseEntity<?> getCurruncy() {
return ResponseEntity.ok(cardBrandsListService.getCardBrandsList().entrySet().stream()
.map(g -> new CardBrandsListDTO(g.getValue())).collect(Collectors.toList()));
}
DTO:
public class CardBrandsListDTO {
private String card_brand;
public String getCard_brand() {
return card_brand;
}
public void setCard_brand(String card_brand) {
this.card_brand = card_brand;
}
}
But I get error: The method entrySet() is undefined for the type ArrayList<String> What is the proper wya to map ArrayList?
Your rest endpoint should look like the following:
#GetMapping("/card_brand/list")
public ResponseEntity<List<CardBrandsListDTO>> getCurruncy() {
return ResponseEntity.ok(cardBrandsListService.getCardBrandsList().stream()
.map(g -> new CardBrandsListDTO(g)).collect(Collectors.toList()));
You are calling entrySet(), which is used to get a Set of entries of a Map object (which you don't have). Additionally, inside the map function, your variable g is a String (since you are returning an ArrayList<String>), therefore you can directly supply that to the constructor. And you can directly set the correct type for the ResponseEntity as well.
UPDATE:
And you need the corresponding constructor:
public class CardBrandsListDTO {
private String card_brand;
public CarBrandsListDTO(String card_brand) {
this.car_brand = car_brand;
}
//getter and setter
}
By the way, I would advise you to rename the DTO (for understandability) and also the field inside it (to follow naming conventions)
I am getting json from dynamoDb that looks like this -
{
"id": "1234",
"payment": {
"payment_id": "2345",
"user_defined": {
"some_id": "3456"
}
}
}
My aim is to get the user_defined field in a Java HashMap<String, Object> as user_defined field can contain any user defined fields, which would be unknown until the data arrives. Everything works fine except my DynamoDBMapper cannot convert the user_defined field to a Java HashMap. It is throwing this error -
Exception occured Response[payment]; could not unconvert attribute
This is how the classes looks like -
#DynamoDBTable(tableName = "PaymentDetails")
public class Response {
private String id;
public Response() {
}
private Payment payment = new Payment();
#DynamoDBHashKey(attributeName="id")
public String getId() { return id; }
public void setId(String id) { this.id = id; }
public Payment getPayment() {
return payment;
}
public void setPayment(Payment payment) {
this.payment = payment;
}
}
The payment field mapper -
#DynamoDBDocument
public class Payment {
private String payment_id:
private HashMap<String, Object> user_defined;
public Payment() {}
public getPayment_id() {
return payment_id;
}
public setPayment_id(String payment_id) {
this.payment_id = payment_id;
}
#DynamoDBTypeConverted(converter = HashMapMarshaller.class)
public HashMap<String, Object> getUser_defined() {
return user_defined;
}
public void setUser_defined(HashMap<String, Object> user_defined) {
this.user_defined = user_defined;
}
}
The HashMapMarshaller(Just to check if Hashmap marshaller wasn't working with gson, I just defined a Hashmap, put in a value and return it, but seems to still not working) -
public class HashMapMarshaller implements DynamoDBTypeConverter<String, HashMap<String, Object>> {
#Override
public String convert(HashMap<String, Object> hashMap) {
return new Gson().toJson(hashMap);
}
#Override
public HashMap<String, Object> unconvert(String jsonString) {
System.out.println("jsonString received for unconverting is " + jsonString);
System.out.println("Unconverting attribute");
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("key", "value");
return hashMap;
//return new Gson().fromJson(jsonString, new TypeToken<HashMap<String, Object>>(){}.getType());
}
}
Marshaller approach is till now not working for me. It is also not printing any of the printlns I've put in there. I've also tried using #DynamoDBTyped(DynamoDBMapperFieldModel.DynamoDBAttributeType.M) and using Map instead of HashMap above my user_defined getter to no avail.
I want to find out how to convert the user_defined field to Java HashMap or Map. Any help is appreciated. Thank you!
Make Map<String, Object> to Map<String, String>. It should work without any custom converters. Otherwise be specific about Map's value type. For example, Map<String, SimplePojo> should work. Don't forget to annotate SimplePojo class with #DynamoDBDocument.
With Object as a type of Map's value, DynamoDB will not able to decide which object it has to create while reading entry from DynamoDB. It should know about specific type like String, Integer, SimplePojo etc.
I am trying to parse a json string to java object but i am not sure on the object hierarchy.
below is the json string
{
"TABLE_Detail":{
"1":{
"TABLE":"table1",
"RUN_DATE":"20170313",
"SEQ_NUM":"123",
"START_TIME":"20170313133144",
"END_TIME":"20170313133655"
},
"2":{
"TABLE":"table2",
"RUN_DATE":"20170313",
"SEQ_NUM":"123",
"START_TIME":"20170313133142",
"END_TIME":"20170313133723"
}
}
}
Here the number 1, 2 are dynamic and can go up to any number, I tried to create a outer object and have a Map of type key String and value as object TableData. The map having variable name TABLE_Detail. but the TableData object is always null. TableData object has all the variables.
Please help me on how to convert this json string to object.
Change 1 to table1 and 2 to table2:
public class TableDetails {
private TableData table1;
private TableData table2;
public TableDetails(){
}
// getter and setter
}
And if modify json format to "Koen Van Looveren" mentioned:
public class TableDetails {
List<TableData> tables;
public TableDetails() {
}
// getter and setter
}
The table class:
Table.java:
public class TableData {
private String table;
private String run_date;
private String seq_num;
private String start_time;
private String end_time;
public TableData() {
}
// getter and setter
}
you have two choice for such painfully json structure when using Gson.
using Gson parsing json as Map and write some class access returned Map.this mode works fine for access data only!
//usage
TableDetails details=new TableDetails(gson.fromJson(json, Map.class));
//classes
class TableDetails {
private Map<String, Map> context;
public TableDetails(Map root) {
this.context = (Map<String, Map>) root.get("TABLE_Detail");
}
public int size() {
return context.size();
}
public Table get(String key) {
return new Table(context.get(key));
}
}
class Table {
private Map context;
public Table(Map context) {
this.context = context;
}
public String getName() {
return get("TABLE");
}
private <T> T get(String name) {
return (T) context.get(name);
}
...
}
write your own Gson TypeAdapter,but this way may be more complex.if you interesting on write custom TypeAdapter there is a demo that I written for extract json root.gson-enclosing-plugin
You can try deserialize it into a Map<String, Map<String, TableData>>. The reason why Map<String, TableData> doesn't work it that the pesudo-array is wrapped in another object.
The following example converts a response into a List<TableData>:
public List<TableData> deserialize(String json) {
return Gson().<Map<String, Map<String, TableData>>>fromJson(json, new TypeToken<Map<String, Map<String, TableData>>>(){}.getType())
.values().iterator().next()
.entrySet().stream()
.sorted(Comparator.comparingInt(e -> Integer.parseInt(e.getKey())))
.map(Map.Entry::getValue)
.collect(Collectors.toList());
}
I was in a search for the solution, and i came across one of the site where the solution worked. i wanted to credit the below site. Thanks for all the support.
I am able to map the dynamic value 1, 2 as map keys and values are mapped correspondingly to the TableData object properties using #SerializedName gson annootation.
http://findnerd.com/list/view/Parse-Json-Object-with-dynamic-keys-using-Gson-/24094/
When using an array in json you need to use [ for opening and ] for closing
{
"TABLE_Detail": [
{
"TABLE": "table1",
"RUN_DATE": "20170313",
"SEQ_NUM": "123",
"START_TIME": "20170313133144",
"END_TIME": "20170313133655"
},
{
"TABLE": "table2",
"RUN_DATE": "20170313",
"SEQ_NUM": "123",
"START_TIME": "20170313133142",
"END_TIME": "20170313133723"
}
]
}
I discovered when saving a POJO with a map field using Firebase on Android, that if that map contains nulls in the value property of that map, then the whole field is ignored.
The workaround is easy (non-null values will result in the map saving successfully), but I want to understand why is this so?
Model
public class Game {
private String owner;
private Map<String, String> characters;
public Game() {
// empty constructor for Firebase
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public Map<String, String> getCharacters() {
return characters;
}
public void setCharacters(Map<String, String> characters) {
this.characters = characters;
}
}
Calling code
final Game game = new Game();
game.setOwner("owner");
game.setCharacters(new HashMap<String, String>());
game.getCharacters().put("Bugs Bunny", null);
game.getCharacters().put("Batman", null);
final Firebase ref = new Firebase("<firebaseurl>/test/" + System.currentTimeMillis());
ref.setValue(game);
Resulting object in Firebase
{
"1456421340781": {
"owner": "owner"
}
}
They're actually not ignored. When you give Firebase a null value for a property/path, you indicate that you want to property or path to be deleted.
From the documentation on Firebase's JavaScript set() method:
Passing null for the new value is equivalent to calling remove(); all data at this location or any child location will be deleted.
So if you set a value with:
ref.child("keith").setValue(47649);
Then the following will delete it:
ref.child("keith").setValue(null);
This behavior is most useful when you use updateChildren(), but it works equally when you call setValue().