• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

convert byte array into List<? extends Document>. here Document is of org.bson.Document type

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to convert a byte array into List<? extends Document>.  here Document is of org.bson.Document type. Any hints please.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
Please show us what you have tried. Do you have some sort of byte array input stream?
 
prashanth prashant
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.json.JSONObject;
import org.bson.Document;

public class ReadJSONExample{

public static void main(String[] args) throws UnsupportedEncodingException {
JSONObject obj = new JSONObject();
   obj.put("name", "foo");
   obj.put("num", 100);
   obj.put("balance", 1000.21);
   obj.put("is_vip", true);
   obj.put("nickname","hi");

   byte[] objAsBytes = obj.toString().getBytes();
   Document doc = convertByteIntoDocument(objAsBytes);
   List<? extends Document> doc2 = (List<? extends Document>) doc;
ReadJSONExample.insertIntoDB(doc2);
   
}


private static void insertIntoDB(List<? extends Document> message) {
// inserting bson Document into DB
}
}
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've converted the document to a String and that into a byte array. Now you need to reverse the process. The String class has a way to convert a byte array to a String. You'd have to research how to convert the String to a document.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Document wouldn't implement Serializable, would it? Can you then deserialise it from a byte[]?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic