frizlab / bsonserialization Goto Github PK
View Code? Open in Web Editor NEWBSON Serialization in Swift
License: MIT License
BSON Serialization in Swift
License: MIT License
Hello @Frizlab,
As per your comments:
"...I’ll work seriously on the project if it gains enough attention. Feel free to open issues, I’ll do my best to answer."""
I'm willing to help where needed. Having said that, would it be beneficial for project to turn "TODO" section into standalone Project ?
That way, we can work together on improving this neat library 😀
Cheers !
There appears to be an issue with how BSONSerialization interacts with Foundation's JSONSerialization (https://developer.apple.com/documentation/foundation/jsonserialization/1415493-jsonobject)
When a JSON String is parsed into a Dictionary via the method JSONSerialization.jsonObject
, if the JSON String contains an int value, JSONSerialization parses it into an NSNumber value instead of into a Boolean value. The problem is that this can be cast as a Int, Double, or a Boolean. Since the write(BSONEntity...)
method first attempts to cast the entity as a boolean, it will succeed, even if the value is an Int, and it gets written out as a Boolean. For example, you can run this from the repl:
import Foundation
import BSONSerialization
let jsonString = "{\"intProperty\": 0}"
let dict = try! JSONSerialization.jsonObject(with: jsonString.data(using: .utf8)!, options: []) as! [String: Any]
let serializedBSONDoc = try BSONSerialization.data(withBSONObject: dict, options: [])
let unserializedBSONDoc = try BSONSerialization.bsonObject(with: serializedBSONDoc, options: [])
->
unserializedBSONDoc: BSONDoc = 1 key/value pair {
[0] = {
key = "intProperty"
value = false
}
}
Please see this Stackoverflow for more details:
https://stackoverflow.com/questions/49641102/using-jsonserialization-to-dynamically-figure-out-boolean-values
I will submit a PR shortly with one possible solution.
Hi
I have a input-stream of bson documents coming from the server.
I was trying to use the bsonObject function from BSONSerialization. But i believe the bson decoder is only able to decode the one document and is failing for all subsequent ones.
My question is it possible to read and serialize multiple documents coming down in a inputstream? Any help will be much appreciated.
Please find my code below.
Alamofire.request(request as URLRequestConvertible).stream { (response) -> Void in
print(response)
do {
let inputStream = InputStream(data: response)
inputStream.open()
let data = try BSONSerialization.bsonObject(with: inputStream)
print(data)
}catch{
print(error)
}
}
@Frizlab I have tried this logic and tried
func searaliseBsonStream() {
let request = self.getRequest()
Alamofire.request(request as URLRequestConvertible).stream { (response) -> Void in
print(response)
let inputStream = InputStream(data: response)
do {
inputStream.open()
while inputStream.hasBytesAvailable {
let data = try BSONSerialization.bsonObject(with: inputStream)
print(data)
}
}
}
}
It works for some time and then throwing the following error
noMoreData
16384 bytes
invalidElementType(114)
16384 bytes
invalidElementType(54)
8768 bytes
invalidElementType(116)
3992 bytes
invalidElementType(115)
However if i put a debugger inside the bsonObject function and run the code, its reading more documents and throwing less errors.
I wonder if there is a thread issue or something.. Appreicate your help.. We been stuck on this for a while now and not sure what to do?
Hi Sorry I had to create a new issue as i couldnt find a way to re open the current issue..
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.