7-Dec-2019 | Like this? Dislike this? Let me know |
Let's set the tone early: This rant is about mainstream use cases. It is not about the ultimate high performance encoding/decoding. It is not about the slickest integration with objects and code generation. This is simply about taking data from point A to point B with high fidelity and the fewest possible technical issues. BSON offers several very important features that make it The Best format for intercomponent data transfer. We will draw comparisons to some other formats but not XML because frankly that ship sailed years ago.
class Invoice { private Money amt; // see separate rant on Money private Date invoiceDate; }
class Invoice { public byte[] toBSON() { Document doc = new Document(); doc.put("amt", amt.getMagnitude()); // gets a BigDecimal doc.put("ccode", amt.getCurrency()); // gets a String doc.put("invoiceDate", invoiceDate); // simply take the java.util.Date return BsonUtils.toBytes(doc); // a convenience func over 3 calls of low level BSON functions } }
Java: public fromBSON(byte[] material) { // could be an InputStream too... Document doc = BsonUtils.fromBytes(material); // doc.amt is a BigDecimal // doc.ccode is a String // doc.invoiceDate is a java.util.Date } Python: def fromBSON(material): doc = bson_utils.fromBytes(material) # doc.amt is decimal # doc.ccode is a unicode string # doc.invoiceDate is a datetime.datetime
Like this? Dislike this? Let me know