Giter Club home page Giter Club logo

bsonserialization's Issues

Further development ?

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 !

Issue with JSONSerialization

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.

BSONDecoder help?

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)
            }
        }

Inputstream failing

@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..

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.