// NewJSONImportInput creates a new JSONImportInput in array mode if specified, // configured to read data to the given io.Reader func NewJSONImportInput(isArray bool, in io.Reader) *JSONImportInput { return &JSONImportInput{ IsArray: isArray, Decoder: json.NewDecoder(in), Reader: in, readerPtr: in, NumImported: 0, readOpeningBracket: false, bytesFromReader: make([]byte, 1), } }
// NewJSONInputReader creates a new JSONInputReader in array mode if specified, // configured to read data to the given io.Reader. func NewJSONInputReader(isArray bool, in io.Reader, numDecoders int) *JSONInputReader { szCount := newSizeTrackingReader(newBomDiscardingReader(in)) return &JSONInputReader{ isArray: isArray, sizeTracker: szCount, decoder: json.NewDecoder(szCount), readOpeningBracket: false, bytesFromReader: make([]byte, 1), numDecoders: numDecoders, } }
// ImportDocument converts the given JSON object to a BSON object func (jsonImporter *JSONImportInput) ImportDocument() (bson.M, error) { var document bson.M if jsonImporter.IsArray { if err := jsonImporter.readJSONArraySeparator(); err != nil { return nil, err } } if err := jsonImporter.Decoder.Decode(&document); err != nil { return nil, err } // reinitialize the reader with data left in the decoder's buffer and the // handle to the underlying reader jsonImporter.Reader = io.MultiReader(jsonImporter.Decoder.Buffered(), jsonImporter.readerPtr) // convert any data produced by mongoexport to the appropriate underlying // extended BSON type. NOTE: this assumes specially formated JSON values // in the input JSON - values such as: // // { $oid: 53cefc71b14ed89d84856287 } // // should be interpreted as: // // ObjectId("53cefc71b14ed89d84856287") // // This applies for all the other extended JSON types MongoDB supports if err := bsonutil.ConvertJSONDocumentToBSON(document); err != nil { return nil, err } jsonImporter.NumImported++ // reinitialize the decoder with its existing buffer and the underlying // reader jsonImporter.Decoder = json.NewDecoder(jsonImporter.Reader) return document, nil }