├── APACHE-2.0.txt ├── README.md ├── SConstruct ├── build ├── fromcsv │ └── fromcsv.vcxproj ├── fromjson │ ├── fromjson.sln │ ├── fromjson.vcxproj │ └── fromjson.vcxproj.filters └── hex │ └── hex.vcxproj ├── doc ├── bson.md ├── fromxml.md └── hex.md ├── src ├── bsontools │ ├── binary.h │ ├── bsonmain.cpp │ ├── cmdline.h │ ├── fromcsv.cpp │ ├── fromjson.cpp │ ├── fromxml.cpp │ ├── hex.cpp │ ├── parse_types.h │ └── stdin.h └── util │ └── str.h └── test └── test.xml /APACHE-2.0.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | These are simple utilities for manipulation of bson documents. 2 | 3 | * bson - main utility. Try bson --help 4 | * fromjson - convert JSON to BSON 5 | * fromcsv - convert CSV to BSON 6 | * fromxml - convert XML to BSON 7 | * hex - hex dump of any input, with a tiny bit of bson format awareness. 8 | also provided as a convenience for any OS where 'hexdump' is not already present. 9 | 10 | Note: Many of these utilities expect input from stdin, to facilitate piping. Use -h to get help. 11 | 12 | ## Documentation 13 | 14 | See the [doc/](doc) folder. 15 | 16 | ## Building 17 | 18 | You will need the [bson-cxx](https://github.com/dwight/bson-cxx) library (a dependancy) to build the tools. It is available on github. 19 | 20 | Place it as a peer level directory on disk with bsontools. (That is you will see lines such as `#include "../../../bson-cxx/"` in the source code of these tools...) 21 | 22 | To build with scons, assuming it is installed, just type "scons". 23 | 24 | With Visual Studio, start by opening build/fromjson/fronjson.sln. The tools have been built and tested with Visual Studio 2013. 25 | 26 | As written, these tools lightly use C++11. This is mainly to avoid any external dependencies; for 27 | example unique_ptr is used from C++11. The usage is light enough you should not need a very new compiler. It would not be hard to adapt back to C++03. Use `-std=c++0x` on the command line, which the SConscript file does for you... 28 | 29 | ### Prebuilt binaries 30 | 31 | Some can be found at [https://github.com/dwight/binaries](https://github.com/dwight/binaries). 32 | 33 | ## Licence 34 | 35 | Apache 2.0. 36 | 37 | ## Contributions 38 | 39 | Send a pull request on github, thanks. We'll pull in things that we think fit although the goal is to keep things tidy and small. Of course you can always fork if you want to create something different. 40 | 41 | Some automated tests would be real helpful for example... 42 | 43 | ## Support 44 | 45 | For help try posting to the [BSON Google Groups forum](https://groups.google.com/forum/#!forum/bson). 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /SConstruct: -------------------------------------------------------------------------------- 1 | env = Environment() 2 | 3 | env.Append(CCFLAGS='-std=c++0x') 4 | 5 | # env.Program(target = 'hex', source = ["src/bsontools/hex.cpp"]) 6 | 7 | env.Program(target = 'hex', source = ["src/bsontools/hex.cpp"]) 8 | 9 | dep1 = [ 10 | "../bson-cxx/src/bson/time_support.cpp", 11 | "../bson-cxx/src/bson/bson.cpp", 12 | "../bson-cxx/src/bson/parse_number.cpp" ] 13 | 14 | dep2 = [ 15 | "../bson-cxx/src/bson/json.cpp", 16 | "../bson-cxx/src/bson/base64.cpp" 17 | ] 18 | 19 | env.Program(target = 'fromcsv', source = ["src/bsontools/fromcsv.cpp"] + dep1) 20 | 21 | env.Program(target = 'fromjson', source = ["src/bsontools/fromjson.cpp"] + dep1 + dep2) 22 | 23 | env.Program(target = 'fromxml', source = ["src/bsontools/fromxml.cpp"] + dep1) 24 | 25 | env.Program(target = 'bson', source = ["src/bsontools/bsonmain.cpp"] + dep1 + dep2) 26 | -------------------------------------------------------------------------------- /build/fromcsv/fromcsv.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {8A26B3AF-95B8-44C0-8AEE-1C3108102466} 15 | Win32Proj 16 | fromcsv 17 | 18 | 19 | 20 | Application 21 | true 22 | v120 23 | Unicode 24 | 25 | 26 | Application 27 | false 28 | v120 29 | true 30 | Unicode 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | true 44 | 45 | 46 | false 47 | 48 | 49 | 50 | 51 | 52 | Level3 53 | Disabled 54 | WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) 55 | true 56 | 57 | 58 | Console 59 | true 60 | 61 | 62 | 63 | 64 | Level3 65 | 66 | 67 | MaxSpeed 68 | true 69 | true 70 | WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) 71 | true 72 | 73 | 74 | Console 75 | true 76 | true 77 | true 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /build/fromjson/fromjson.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.30501.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "fromjson", "fromjson.vcxproj", "{B3EB1C1F-AB6D-40D1-91B4-942CBC0C2B68}" 7 | EndProject 8 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "hex", "..\hex\hex.vcxproj", "{611CB369-3FF0-4F24-956F-C76E07856CE9}" 9 | EndProject 10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{9A3B428C-5FB4-4023-AF79-7CA8EE07214C}" 11 | ProjectSection(SolutionItems) = preProject 12 | ..\..\README.md = ..\..\README.md 13 | EndProjectSection 14 | EndProject 15 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "bson", "..\bson\bson.vcxproj", "{E0893BE6-2391-4890-BBD3-5AD4B9ED1C15}" 16 | EndProject 17 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "fromcsv", "..\fromcsv\fromcsv.vcxproj", "{8A26B3AF-95B8-44C0-8AEE-1C3108102466}" 18 | EndProject 19 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "fromxml", "..\fromxml\fromxml.vcxproj", "{9AC09F80-F7ED-45B4-BAB1-48B75F4616F7}" 20 | EndProject 21 | Global 22 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 23 | Debug|Win32 = Debug|Win32 24 | Release|Win32 = Release|Win32 25 | EndGlobalSection 26 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 27 | {B3EB1C1F-AB6D-40D1-91B4-942CBC0C2B68}.Debug|Win32.ActiveCfg = Debug|Win32 28 | {B3EB1C1F-AB6D-40D1-91B4-942CBC0C2B68}.Debug|Win32.Build.0 = Debug|Win32 29 | {B3EB1C1F-AB6D-40D1-91B4-942CBC0C2B68}.Release|Win32.ActiveCfg = Release|Win32 30 | {B3EB1C1F-AB6D-40D1-91B4-942CBC0C2B68}.Release|Win32.Build.0 = Release|Win32 31 | {611CB369-3FF0-4F24-956F-C76E07856CE9}.Debug|Win32.ActiveCfg = Debug|Win32 32 | {611CB369-3FF0-4F24-956F-C76E07856CE9}.Debug|Win32.Build.0 = Debug|Win32 33 | {611CB369-3FF0-4F24-956F-C76E07856CE9}.Release|Win32.ActiveCfg = Release|Win32 34 | {611CB369-3FF0-4F24-956F-C76E07856CE9}.Release|Win32.Build.0 = Release|Win32 35 | {E0893BE6-2391-4890-BBD3-5AD4B9ED1C15}.Debug|Win32.ActiveCfg = Debug|Win32 36 | {E0893BE6-2391-4890-BBD3-5AD4B9ED1C15}.Debug|Win32.Build.0 = Debug|Win32 37 | {E0893BE6-2391-4890-BBD3-5AD4B9ED1C15}.Release|Win32.ActiveCfg = Release|Win32 38 | {E0893BE6-2391-4890-BBD3-5AD4B9ED1C15}.Release|Win32.Build.0 = Release|Win32 39 | {8A26B3AF-95B8-44C0-8AEE-1C3108102466}.Debug|Win32.ActiveCfg = Debug|Win32 40 | {8A26B3AF-95B8-44C0-8AEE-1C3108102466}.Debug|Win32.Build.0 = Debug|Win32 41 | {8A26B3AF-95B8-44C0-8AEE-1C3108102466}.Release|Win32.ActiveCfg = Release|Win32 42 | {8A26B3AF-95B8-44C0-8AEE-1C3108102466}.Release|Win32.Build.0 = Release|Win32 43 | {9AC09F80-F7ED-45B4-BAB1-48B75F4616F7}.Debug|Win32.ActiveCfg = Debug|Win32 44 | {9AC09F80-F7ED-45B4-BAB1-48B75F4616F7}.Debug|Win32.Build.0 = Debug|Win32 45 | {9AC09F80-F7ED-45B4-BAB1-48B75F4616F7}.Release|Win32.ActiveCfg = Release|Win32 46 | {9AC09F80-F7ED-45B4-BAB1-48B75F4616F7}.Release|Win32.Build.0 = Release|Win32 47 | EndGlobalSection 48 | GlobalSection(SolutionProperties) = preSolution 49 | HideSolutionNode = FALSE 50 | EndGlobalSection 51 | EndGlobal 52 | -------------------------------------------------------------------------------- /build/fromjson/fromjson.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | {B3EB1C1F-AB6D-40D1-91B4-942CBC0C2B68} 49 | Win32Proj 50 | fromjson 51 | 52 | 53 | 54 | Application 55 | true 56 | v120 57 | MultiByte 58 | 59 | 60 | Application 61 | false 62 | v120 63 | true 64 | MultiByte 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | true 78 | 79 | 80 | false 81 | 82 | 83 | 84 | 85 | 86 | Level3 87 | Disabled 88 | WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) 89 | true 90 | 91 | 92 | Console 93 | true 94 | 95 | 96 | 97 | 98 | Level3 99 | 100 | 101 | MaxSpeed 102 | true 103 | true 104 | WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) 105 | true 106 | 107 | 108 | Console 109 | true 110 | true 111 | true 112 | 113 | 114 | 115 | 116 | 117 | -------------------------------------------------------------------------------- /build/fromjson/fromjson.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | {65756162-fdb6-40ea-a7b2-5105fb9bfbfe} 18 | 19 | 20 | 21 | 22 | Source Files 23 | 24 | 25 | bson 26 | 27 | 28 | bson 29 | 30 | 31 | bson 32 | 33 | 34 | bson 35 | 36 | 37 | bson 38 | 39 | 40 | bson 41 | 42 | 43 | 44 | 45 | Header Files 46 | 47 | 48 | Header Files 49 | 50 | 51 | Header Files 52 | 53 | 54 | Header Files 55 | 56 | 57 | Header Files 58 | 59 | 60 | Header Files 61 | 62 | 63 | Header Files 64 | 65 | 66 | Header Files 67 | 68 | 69 | Header Files 70 | 71 | 72 | Header Files 73 | 74 | 75 | Header Files 76 | 77 | 78 | Header Files 79 | 80 | 81 | Header Files 82 | 83 | 84 | Header Files 85 | 86 | 87 | Header Files 88 | 89 | 90 | Header Files 91 | 92 | 93 | Header Files 94 | 95 | 96 | Header Files 97 | 98 | 99 | Header Files 100 | 101 | 102 | Header Files 103 | 104 | 105 | Header Files 106 | 107 | 108 | Header Files 109 | 110 | 111 | Header Files 112 | 113 | 114 | -------------------------------------------------------------------------------- /build/hex/hex.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {611CB369-3FF0-4F24-956F-C76E07856CE9} 15 | Win32Proj 16 | hex 17 | 18 | 19 | 20 | Application 21 | true 22 | v120 23 | Unicode 24 | 25 | 26 | Application 27 | false 28 | v120 29 | true 30 | Unicode 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | true 44 | 45 | 46 | false 47 | 48 | 49 | 50 | 51 | 52 | Level3 53 | Disabled 54 | WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) 55 | true 56 | 57 | 58 | Console 59 | true 60 | 61 | 62 | 63 | 64 | Level3 65 | 66 | 67 | MaxSpeed 68 | true 69 | true 70 | WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) 71 | true 72 | 73 | 74 | Console 75 | true 76 | true 77 | true 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /doc/bson.md: -------------------------------------------------------------------------------- 1 | The "bson" utility program. 2 | 3 | ## Usage 4 | 5 | bson \ \ \ 6 | 7 | Options: 8 | 9 | -h for help 10 | -# emit document number instead of the document's 11 | bson content 12 | -N emit the null value rather than omitting a 13 | missing field, when applicable 14 | (e.g. with the pull command) 15 | 16 | 17 | The bson utility performs many different operations; these are specified by the \ 18 | parameter. Typically, the utility takes a stream of BSON from stdin, and writes BSON to 19 | stdout. However, in some cases, it outputs text, for example, the print verb/command does 20 | that. 21 | 22 | ## Commands 23 | 24 | ### count 25 | 26 | Count number of documents in the BSON file. Also prints out the size of the largest document. 27 | 28 | $ bson count < file.bson 29 | 302 12800 30 | 31 | ### del \ 32 | 33 | Delete a top level field and then reoutput the stream. 34 | 35 | $ bson del card_number < records.bson > redacted.bson 36 | 37 | ### demote \ 38 | 39 | Wrap each document inside a field named , then output. 40 | 41 | For example, suppose we have a set of JSON documents of the form: 42 | 43 | { street:..., city:..., province:..., country:... } 44 | ... 45 | 46 | If we wanted this to be in a larger document we might use the demote command to "wrap" these 47 | values: 48 | 49 | bson demote addr < in.bson > out.bson 50 | 51 | If we then inspected: 52 | 53 | bson print < out.bson 54 | 55 | we would see something like: 56 | 57 | { addr : { street:..., city:..., province:..., country:... } } 58 | ... 59 | 60 | And this output could be used then with say, a merge operation. 61 | 62 | ### grep \ 63 | 64 | Search for a pattern within each documents. Field values, and not the field names, are checked 65 | for a match. All matching documents are output (as BSON). 66 | 67 | $ bson grep foo < file.bson > out.bson 68 | 69 | Note: at the time of this writing, pattern can only be a simple text pattern, regular 70 | expressions are NOT yet supported (but hopefully coming). 71 | 72 | ### head [-n \] [-s \] 73 | 74 | Output the first N documents in the file. Output is BSON format. 75 | 76 | -s option: start at the specified document number (base zero). 77 | 78 | ### infer 79 | 80 | The infer command will take a stream of BSON from stdin and infer its "schema"; that is, it 81 | will report what fields are present in documents read from the stream, and the types of those 82 | fields. If a field is not always present, infer will report the percentage of the time it 83 | occurs. If a field's values have different types in different documents, infer will report 84 | this too. The output from infer is a text report. 85 | 86 | Example: 87 | 88 | $ bson infer < x.bson 89 | { 90 | a : int32 91 | b : bool 50% 92 | c : bool 50%, null 50% 93 | d : string 50% 94 | e : object { 95 | valid : bool 96 | until : date 97 | } 98 | } 99 | 100 | ### merge \ 101 | 102 | Merge bson stream stdin with bson file on cmd line. Must be same number of documents in 103 | each input file. 104 | 105 | ### print 106 | 107 | $ bson print < mydata.bson 108 | *json output is displayed...* 109 | 110 | $ bson print pretty < mydata.bson 111 | *json output is displayed with indentation and line feeds...* 112 | 113 | ### project \ 114 | 115 | Do projection on the input BSON, outputing the result. Only top level field names may be 116 | specified in the current implementation. 117 | 118 | $ bson project "last name","first_name",phone < customers.bson > phonelist.bson 119 | 120 | ### promote \ 121 | 122 | Pull out the subobject specified by and output it (only). If the field is missing 123 | or not an object nothing will be output for that document. 124 | 125 | $ bson promote associate.address < things.bson > addresses.bson 126 | 127 | ### pull [-N] \ 128 | 129 | Extract a single field as a singleton element and output. 130 | 131 | -N option: emit null rather than nothing, when applicable. 132 | 133 | Example: 134 | 135 | $ bson print < t.bson 136 | { a: 3, b: true, c: { x: 3, y: 4 } } 137 | { a: 2, c: { x: 2, y: 9 } } 138 | { c: null } 139 | { a: 1 } 140 | 141 | $ bson pull a < t.bson | bson print 142 | { a: 3 } 143 | { a: 2 } 144 | {} 145 | { a: 1 } 146 | 147 | bson pull c < t.bson | bson print 148 | { c: { x: 3, y: 4 } } 149 | { c: { x: 2, y: 9 } } 150 | { c: null } 151 | {} 152 | 153 | pull c.y < t.bson | bson print 154 | { y: 4 } 155 | { y: 9 } 156 | {} 157 | {} 158 | 159 | ### sample -\ 160 | 161 | sample outputs every Nth document in the file for sampling purposes. Output is bson format. 162 | 163 | $ bson sample -n 100 < bigfile.bson > sample.bson 164 | 165 | ### tail [-n ] 166 | 167 | Output the last N documents. 168 | 169 | Note: the implementation of tail is currently not very fast. 170 | 171 | ### text 172 | 173 | Extract and output the text of each BSON document (perhaps for use then with text utilities etc.) 174 | 175 | The text from each document is output one line per document, with the text of each field tab 176 | delimited. Field names are not output. Numbers are output in their text representation. 177 | 178 | $ # how many docs contain the text "Joseph"? 179 | $ bson text < docs.bson | grep Joseph | wc 180 | 181 | ### unwind [-N] \ 182 | 183 | Unwind an array of elements. This is similar to the MongoDB $unwind operator. 184 | 185 | `-N` option: emit null rather than nothing, when applicable. 186 | 187 | Example: 188 | 189 | $ fromjson > a.bson 190 | { a : 3, b : [1,2,7,8,"zzz"] } 191 | { b : [4,7] } 192 | ^Z 193 | 194 | $ bson print < a.bson 195 | { a: 3, b: [ 1, 2, 7, 8, "zzz" ] } 196 | { b: [ 4, 7 ] } 197 | 198 | $ bson unwind b < a.bson | bson print 199 | { b: 1 } 200 | { b: 2 } 201 | { b: 7 } 202 | { b: 8 } 203 | { b: "zzz" } 204 | { b: 4 } 205 | { b: 7 } 206 | 207 | ## Other Options 208 | 209 | `-#` option: tells the utility to output document numbers rather than the actual document as 210 | output. Simply used for troubleshooting; say, if you are getting an error mid-file and want 211 | to track it down. 212 | 213 | ## Other Notes 214 | 215 | * You can use the regular Unix 'cat' utility to concatenate BSON files, as it simply concatenates streams of bytes. 216 | 217 | -------------------------------------------------------------------------------- /doc/fromxml.md: -------------------------------------------------------------------------------- 1 | fromxml utility -- converts XML to BSON. 2 | 3 | NOTE: PRELIMINARY IMPLEMENTATION (at time of this writing). 4 | 5 | ## Usage 6 | 7 | fromxml < infile.xml > outfile.bson 8 | 9 | Run fromxml -h for options. 10 | 11 | ### Interpretation of structure 12 | 13 | (Note this may evolve, but this version...) 14 | 15 | asdf 16 | 17 | becomes: 18 | 19 | { foo: "asdf" } 20 | 21 | asdf becomes: 22 | 23 | { foo: { x: "33.3", foo: "asdf" } } 24 | 25 | 3asdf becomes: 26 | 27 | { a: { b: "3", c: "asdf" } } 28 | 29 | ### Numerics 30 | 31 | Use -t or -f =options to set rules for interpretation of datatypes. 32 | 33 | 3 34 | 35 | would by default yield: { x : "3" } 36 | 37 | with "-t n": { x : 3 } (where the 3 is an int32) 38 | 39 | with "-t f": { x : 3.0 } 40 | 41 | That is to say the 'n' option uses the smallest type that makes sense. 42 | 43 | More data types will be done later hopefully. 44 | -------------------------------------------------------------------------------- /doc/hex.md: -------------------------------------------------------------------------------- 1 | hex - BSON-friendly hex dump utility. 2 | 3 | ## Usage 4 | 5 | hex < infile.bson 6 | 7 | Options: 8 | 9 | * -h help 10 | * -v verbose style dump 11 | * -raw raw hex dump with no 'bson awareness' 12 | 13 | ## Purpose 14 | 15 | This is a very simple hex dump utility which could be used 16 | with any binary file. However, adds a tiny amount of BSON 17 | awareness, which can be useful when dumping larger BSON files. 18 | 19 | Specifically, it is aware that the first four bytes of a BSON 20 | document indicate its size. It will output the dump of those bytes 21 | on a line by itself, then, the rest of that document, then, a blank 22 | separator line. Thus it makes it easy to find the point of demarcation 23 | for each BSON document in the dump. Trivial but very useful. 24 | 25 | ## Example 26 | 27 | ~/bsontools $ ./fromjson > out.bson 28 | {"hello": "world"} 29 | {"BSON": ["awesome", 5.05, 1986]} 30 | 31 | ~/bsontools $ ./hex < out.bson 32 | 16 00 00 00 33 | 02 68 65 6C 6C 6F 00 06 00 00 00 77 6F 72 6C 64 34 | 00 00 35 | 36 | 31 00 00 00 37 | 04 42 53 4F 4E 00 26 00 00 00 02 30 00 08 00 00 38 | 00 61 77 65 73 6F 6D 65 00 01 31 00 33 33 33 33 39 | 33 33 14 40 10 32 00 C2 07 00 00 00 00 40 | 41 | ~/bsontools $ ./hex -v < out.bson 42 | 16 00 00 00 22 43 | 02 68 65 6C 6C 6F 00 06 00 00 00 77 6F 72 6C 64 .hello.....world 44 | 00 00 .. 45 | 46 | 31 00 00 00 49 47 | 04 42 53 4F 4E 00 26 00 00 00 02 30 00 08 00 00 .BSON.&....0.... 48 | 00 61 77 65 73 6F 6D 65 00 01 31 00 33 33 33 33 .awesome..1.3333 49 | 33 33 14 40 10 32 00 C2 07 00 00 00 00 33.@.2....... 50 | 51 | ~/bsontools $ ./hex -raw < out.bson 52 | 16 00 00 00 02 68 65 6C 6C 6F 00 06 00 00 00 77 53 | 6F 72 6C 64 00 00 31 00 00 00 04 42 53 4F 4E 00 54 | 26 00 00 00 02 30 00 08 00 00 00 61 77 65 73 6F 55 | 6D 65 00 01 31 00 33 33 33 33 33 33 14 40 10 32 56 | 00 C2 07 00 00 00 00 57 | 58 | ~/bsontools $ 59 | -------------------------------------------------------------------------------- /src/bsontools/binary.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "fcntl.h" 4 | #if defined(_WIN32) 5 | #include "io.h" 6 | #endif 7 | #include 8 | 9 | inline void binaryStdIn() { 10 | #if !defined(_WIN32) 11 | freopen(NULL, "rb", stdin); 12 | #else 13 | _setmode(_fileno(stdin), _O_BINARY); 14 | #endif 15 | } 16 | 17 | inline void binaryStdOut() { 18 | #if !defined(_WIN32) 19 | freopen(NULL, "wb", stdout); 20 | #else 21 | _setmode(_fileno(stdout), _O_BINARY); 22 | #endif 23 | } 24 | 25 | class bsontool_error : public std::exception { 26 | std::string msg; 27 | public: 28 | virtual ~bsontool_error() throw() {} 29 | bsontool_error(const char *p) : msg(p) { } 30 | virtual const char* what() const throw() { return msg.c_str(); } 31 | }; 32 | 33 | -------------------------------------------------------------------------------- /src/bsontools/bsonmain.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "binary.h" 4 | #include "../../../bson-cxx/src/bson/bsonobj.h" 5 | #include "../../../bson-cxx/src/bson/bsonobjiterator.h" 6 | #include "../../../bson-cxx/src/bson/bsonobjbuilder.h" 7 | #include "cmdline.h" 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | using namespace _bson; 14 | 15 | #include "stdin.h" 16 | 17 | void strSplit(vector &line, const string& str); 18 | 19 | namespace bsontools { 20 | 21 | bool emitDocNumber = false; // -# option emits doc # instead of BSON content 22 | bool emitNull = false; 23 | 24 | class bsonobjholder { 25 | string buf; 26 | public: 27 | bsonobjholder(bsonobj& o) : buf(o.objdata(), o.objsize()) { } 28 | bsonobj obj() const { return bsonobj(buf.c_str()); } 29 | }; 30 | 31 | void write(const bsonobj& o, long long docNumber = -1) { 32 | if (emitDocNumber && docNumber >= 0) 33 | cout << docNumber << '\n'; 34 | else 35 | cout.write(o.objdata(), o.objsize()); 36 | } 37 | 38 | /** helper class to specialize from when making a new command. */ 39 | class bsonpipe; 40 | map verbs; 41 | class bsonpipe : public StdinDocReader { 42 | protected: 43 | void write(const bsonobj& o) { 44 | bsontools::write(o, nDocs()); 45 | } 46 | // things to override 47 | virtual bool binaryOutput() { return true; } 48 | virtual void finished() { } 49 | virtual bool doc(bsonobj&) = 0; 50 | public: 51 | virtual void init(cmdline&) { } 52 | virtual string help() = 0; 53 | explicit bsonpipe(string name) { 54 | assert(!name.empty()); 55 | verbs.insert(pair(name, this)); 56 | } 57 | virtual void go() { 58 | if (binaryOutput() && !emitDocNumber) 59 | binaryStdOut(); 60 | StdinDocReader::go(); 61 | } 62 | }; 63 | 64 | // infer --------------------------------------------- 65 | 66 | const char *typestrs[] = { 67 | "EOO", 68 | "double", 69 | "string", 70 | "object", "array", "bindata", "undefined", "ObjectID", "bool", "date", "null", "regex", 71 | "DBRef", "Code", "Symbol", "CodeWScope", "int32", "Timestamp", "int64" 72 | }; 73 | 74 | bool hasWeirdChars(const string& s) { 75 | for (auto i = s.begin(); i != s.end(); i++) { 76 | char ch = *i; 77 | if (ch < 48) 78 | return true; 79 | } 80 | return false; 81 | } 82 | 83 | // this is an experimental / prototype dom thing. some aspects are inefficient as still just experimenting 84 | unsigned long long ndocs; 85 | class dom { 86 | public: 87 | class node { 88 | public: 89 | node(string s) : fieldName(s) {} 90 | string fieldName; 91 | virtual void emit(bsonobjbuilder& b) = 0; 92 | virtual void print(int) = 0; 93 | map typeOccurrences; 94 | void printType() { 95 | cout << " : "; 96 | for (auto i = typeOccurrences.begin(); i != typeOccurrences.end(); i++) { 97 | if (i != typeOccurrences.begin()) 98 | cout << ", "; 99 | int t = i->first; 100 | if (t >= 0 && t <= JSTypeMax) { 101 | cout << typestrs[t]; 102 | } 103 | else { 104 | cout << t; 105 | } 106 | if (i->second != ndocs) { 107 | cout << ' ' << (100*i->second) / ndocs<< '%'; 108 | } 109 | } 110 | } 111 | void printFieldname(int L) { 112 | for (int i = -1; i < L; i++) 113 | cout << " "; 114 | if (hasWeirdChars(fieldName)) 115 | cout << '"' << fieldName << '"'; 116 | else 117 | cout << fieldName; 118 | } 119 | }; 120 | class simple : public node { 121 | public: 122 | simple(string s) : node(s){} 123 | void print(int L) { 124 | printFieldname(L); 125 | printType(); 126 | cout << '\n'; 127 | } 128 | void emit(bsonobjbuilder& b) { 129 | b.append(fieldName, 1); 130 | } 131 | }; 132 | class obj : public node { 133 | bool have(const string& fieldName) { 134 | return fields.count(fieldName) > 0; 135 | } 136 | map fields; 137 | vector< unique_ptr > children; 138 | public: 139 | void print(int L) { 140 | if (L >= 0) { 141 | printFieldname(L); 142 | printType(); 143 | cout << ' '; 144 | } 145 | cout << "{\n"; 146 | for (auto i = children.begin(); i != children.end(); i++) { 147 | node* n = i->get(); 148 | n->print(L+1); 149 | } 150 | for (int i = -1; i < L; i++) 151 | cout << " "; 152 | cout << "}\n"; 153 | } 154 | obj(string s) : node(s) {} 155 | const vector< unique_ptr >& getChildren() const { 156 | return children; 157 | } 158 | void emit(bsonobjbuilder& b) { 159 | bsonobjbuilder sub(b.subobjStart(fieldName)); 160 | for (auto i = children.begin(); i != children.end(); i++) { 161 | node* n = i->get(); 162 | n->emit(sub); 163 | } 164 | sub.done(); 165 | } 166 | void addSimple(string fieldName, BSONType t) { 167 | if (!have(fieldName)) { 168 | simple *s = new simple(fieldName); 169 | fields[fieldName] = s; 170 | children.push_back(unique_ptr(s)); 171 | } 172 | (fields[fieldName])->typeOccurrences[t]++; 173 | } 174 | obj& addObj(string fieldName, BSONType t) { 175 | node *o = fields[fieldName]; 176 | if (o == 0) { 177 | o = new obj(fieldName); 178 | fields[fieldName] = o; 179 | children.push_back(unique_ptr(o)); 180 | } 181 | obj *op = (obj *)o; 182 | op->typeOccurrences[t]++; 183 | return *op; 184 | } 185 | }; 186 | obj top; 187 | dom() : top("top") {} 188 | }; 189 | 190 | bool anArrayFieldName(const char *p) { 191 | if (*p == '0' && p[1] == 0) { 192 | // special case, we process that one for inference purposes 193 | return false; 194 | } 195 | if (*p == 0) 196 | return false; 197 | while (*p) { 198 | if (!isdigit(*p)) 199 | return false; 200 | p++; 201 | } 202 | return true; 203 | } 204 | 205 | void go(const bsonobj& o, dom::obj& n, bool arr = false) { 206 | bsonobjiterator i(o); 207 | while (i.more()) { 208 | bsonelement e = i.next(); 209 | if (!e.isObject() ) { 210 | const char *p = e.fieldName(); 211 | if (!arr || !anArrayFieldName(p)) { 212 | n.addSimple(p, e.type()); 213 | } 214 | else { 215 | // we don't want a type report for every array element position! 216 | // thus this. 217 | n.addSimple("0", e.type()); 218 | } 219 | } 220 | else { 221 | dom::obj& x = n.addObj(e.fieldName(), e.type()); 222 | go(e.object(), x, e.type() == Array); 223 | } 224 | } 225 | } 226 | 227 | class infer : public bsonpipe { 228 | virtual bool binaryOutput() { return false; } 229 | virtual string help() { 230 | return "infer infer schema"; 231 | } 232 | virtual bool doc(bsonobj& b) { 233 | infer_go(b); 234 | return true; 235 | } 236 | virtual void init(cmdline& c) { 237 | } 238 | void finished() { 239 | /*bsonobjbuilder b; 240 | d.top.emit(b); 241 | write(b.obj());*/ 242 | ndocs = nDocs(); 243 | d.top.print(-1); 244 | } 245 | dom d; 246 | public: 247 | void infer_go(bsonobj o) { 248 | bsontools::go(o, d.top); 249 | } 250 | infer() : bsonpipe("infer") {} 251 | } _infer; 252 | 253 | /** ----- functors we use when traversing a heirarchy with descent(). */ 254 | 255 | class GrepElem { 256 | public: 257 | bool i = false; 258 | string target; 259 | bool matched = false; 260 | void reset() { matched = false; } 261 | void operator() (bsonelement& x, const bsonobj &parent) { 262 | string s; 263 | const char *p; 264 | if (x.isNumber()) { 265 | s = x.toString(false); 266 | p = s.c_str(); 267 | } 268 | else if (x.type() == _bson::String) { 269 | p = x.valuestr(); 270 | } 271 | else { 272 | return; 273 | } 274 | if (strstr(p, target.c_str()) != 0) 275 | matched = true; 276 | } 277 | }; 278 | 279 | class PrintElem { 280 | public: 281 | void reset() { 282 | s.reset(); 283 | } 284 | bool operator() (bsonelement& x, const bsonobj &parent) { 285 | if (x.type() == _bson::String) { 286 | s << x.valuestr() << '\t'; 287 | } 288 | else { // if (x.isNumber()) { 289 | x.toString(s, false, false); 290 | s << '\t'; 291 | } 292 | return true; 293 | } 294 | StringBuilder s; 295 | }; 296 | 297 | // descent an object's contents recursively. 298 | template 299 | void descend(const bsonobj& o, T& f) { 300 | bsonobjiterator i(o); 301 | while (i.more()) { 302 | bsonelement e = i.next(); 303 | if (e.isObject()) { 304 | descend(e.object(),f); 305 | } 306 | else { 307 | f(e, o); 308 | } 309 | } 310 | } 311 | 312 | class grep : public StdinDocReader { 313 | public: 314 | GrepElem f; 315 | virtual bool doc(bsonobj& b) { 316 | descend(b, f); 317 | if (f.matched) { 318 | write(b, nDocs()); 319 | } 320 | f.reset(); 321 | return true; 322 | } 323 | grep(string s) { 324 | f.target = s; 325 | } 326 | }; 327 | 328 | class text : public StdinDocReader { 329 | public: 330 | PrintElem f; 331 | virtual bool doc(bsonobj& b) { 332 | descend(b, f); 333 | cout << f.s.str() << '\n'; 334 | f.reset(); 335 | return true; 336 | } 337 | }; 338 | 339 | class print : public StdinDocReader { 340 | bool _pretty; 341 | void pretty(const bsonobj& b, string indent="") { 342 | bsonobjiterator i(b); 343 | cout << "{\n"; 344 | while (i.more()) { 345 | bsonelement e = i.next(); 346 | if (e.isObject()) { 347 | cout << indent << " " << e.fieldName() << ": "; 348 | string s = indent + " "; 349 | pretty(e.object(), s); 350 | } 351 | else { 352 | cout << indent << " " << e.toString(true, true); 353 | if (i.more()) 354 | cout << ','; 355 | cout << '\n'; 356 | } 357 | } 358 | cout << indent << "}\n"; 359 | } 360 | public: 361 | virtual bool doc(bsonobj& b) { 362 | if (_pretty) { 363 | pretty(b); 364 | } 365 | else { 366 | cout << b.toString(false, true); 367 | } 368 | cout << endl; 369 | return true; 370 | } 371 | void go(cmdline& c) { 372 | _pretty = c.second() == "pretty"; 373 | StdinDocReader::go(); 374 | } 375 | }; 376 | 377 | class count : public StdinDocReader { 378 | public: 379 | int largest = 0; 380 | virtual bool doc(bsonobj& b) { 381 | if (b.objsize() > largest) 382 | largest = b.objsize(); 383 | return true; 384 | } 385 | }; 386 | 387 | /** THIS IS VERY INEFFICIENT. But wanted to just get the capability in a crude form in place. */ 388 | class tail : public StdinDocReader { 389 | int n = 0; 390 | deque last; 391 | virtual void finished() { 392 | for (auto i = last.begin(); i != last.end(); i++) { 393 | bsonobj o = i->obj(); 394 | write(o); 395 | } 396 | } 397 | virtual bool doc(bsonobj& b) { 398 | if (++n > N) { 399 | last.pop_front(); 400 | } 401 | last.push_back(bsonobjholder(b)); 402 | return true; 403 | } 404 | public: 405 | int N = 10; 406 | tail() { 407 | binaryStdOut(); 408 | } 409 | }; 410 | 411 | class sample : public StdinDocReader { 412 | virtual bool doc(bsonobj& b) { 413 | if (nDocs() % N == 0) { 414 | write(b, nDocs()); 415 | } 416 | return true; 417 | } 418 | public: 419 | int N = 10; 420 | sample() { 421 | binaryStdOut(); 422 | } 423 | }; 424 | 425 | class project : public StdinDocReader { 426 | virtual bool doc(bsonobj& b) { 427 | set f(fields.begin(), fields.end()); 428 | { 429 | bsonobjbuilder bldr; 430 | bsonobjiterator i(b); 431 | while (i.more()) { 432 | bsonelement e = i.next(); 433 | if (f.count(e.fieldName())) { 434 | bldr.append(e); 435 | } 436 | } 437 | write(bldr.obj(), nDocs()); 438 | } 439 | return true; 440 | } 441 | public: 442 | vectorfields; 443 | project() { binaryStdOut(); } 444 | }; 445 | 446 | class del : public StdinDocReader { 447 | virtual bool doc(bsonobj& b) { 448 | if (!b.hasField(fieldName)) { 449 | write(b, nDocs()); 450 | } 451 | else { 452 | bsonobjbuilder bldr; 453 | bsonobjiterator i(b); 454 | while (i.more()) { 455 | bsonelement e = i.next(); 456 | if (fieldName != e.fieldName()) { 457 | bldr.append(e); 458 | } 459 | } 460 | write(bldr.obj(), nDocs()); 461 | } 462 | return true; 463 | } 464 | public: 465 | string fieldName; 466 | del() { binaryStdOut(); } 467 | }; 468 | 469 | class unwind : public bsonpipe { 470 | virtual string help() { 471 | return "unwind unwind array (or object) elements. dot notation ok."; 472 | } 473 | virtual bool doc(bsonobj& b) { 474 | bsonelement e = b.getFieldDotted(fieldName); 475 | if (e.isObject()) { 476 | bsonobjiterator i(e.object()); 477 | while (i.more()) { 478 | bsonobjbuilder bldr; 479 | bldr.appendAs(i.next(), fieldName); 480 | write(bldr.obj()); 481 | } 482 | } 483 | else if (emitNull) { 484 | bsonobjbuilder bldr; 485 | bldr.appendNull(fieldName); 486 | write(bldr.obj()); 487 | } 488 | else { 489 | //write(bsonobj()); 490 | } 491 | return true; 492 | } 493 | string fieldName; 494 | virtual void init(cmdline& c) { 495 | fieldName = c.second(); 496 | } 497 | public: 498 | unwind() : bsonpipe("unwind") {} 499 | } _unwind; 500 | 501 | class pull : public bsonpipe { 502 | virtual string help() { 503 | return "pull extract a single field as a singleton element in output documents. dot notation ok."; 504 | } 505 | virtual bool doc(bsonobj& b) { 506 | bsonobjbuilder bldr; 507 | bsonelement e = b.getFieldDotted(fieldName); 508 | if (!e.eoo()) 509 | bldr.append(e); 510 | else if (emitNull) 511 | bldr.appendNull(fieldName); 512 | write(bldr.obj()); 513 | return true; 514 | } 515 | string fieldName; 516 | virtual void init(cmdline& c) { 517 | fieldName = c.second(); 518 | } 519 | public: 520 | pull() : bsonpipe("pull") {} 521 | } _pull; 522 | 523 | class demote : public bsonpipe { 524 | virtual string help() { 525 | return "demote put each document inside a field named fieldname, then output"; 526 | } 527 | virtual bool doc(bsonobj& b) { 528 | bsonobjbuilder bldr; 529 | bldr.append(fieldName, b); 530 | write(bldr.obj()); 531 | return true; 532 | } 533 | string fieldName; 534 | void init(cmdline& c) { 535 | fieldName = c.second(); 536 | } 537 | public: 538 | demote() : bsonpipe("demote") {} 539 | } _demote; 540 | 541 | void merge(istream& _a, istream& _b) { 542 | DocReader a(_a); 543 | DocReader b(_b); 544 | while (1) { 545 | bsonobjbuilder builder; 546 | int r1 = a.next(); 547 | int r2 = b.next(); 548 | if (r1 > 0) 549 | exit(r1); 550 | if (r2 > 0) 551 | exit(r2); 552 | if (r1 != r2) { 553 | cerr << "error: different number of documents in two files to merge" << endl; 554 | exit(5); 555 | } 556 | if (r1 < 0) 557 | break; 558 | 559 | // cout << '\n' << b.obj.toString() << endl; 560 | 561 | // ok... 562 | builder.appendElements(a.obj); 563 | builder.appendElementsUnique(b.obj); 564 | write(builder.obj(), a.nDocs()); 565 | } 566 | } 567 | 568 | class promote : public StdinDocReader { 569 | virtual bool doc(bsonobj& b) { 570 | //cout << b.toString() << endl; 571 | bsonelement e = b.getFieldDotted(fieldName); 572 | if (e.isObject()) { 573 | write(e.object(), nDocs()); 574 | } 575 | return true; 576 | } 577 | public: 578 | string fieldName; 579 | promote() { binaryStdOut(); } 580 | }; 581 | 582 | class head : public StdinDocReader { 583 | unsigned long long n = 0; 584 | unsigned long long nOutput = 0; 585 | virtual bool doc(bsonobj& b) { 586 | if (n >= startAt) { 587 | write(b, nDocs()); 588 | nOutput++; 589 | if (nOutput >= N) 590 | done = true; 591 | } 592 | n++; 593 | return true; 594 | } 595 | public: 596 | unsigned long long startAt = 0; 597 | unsigned long long N = 10; 598 | head() { 599 | binaryStdOut(); 600 | } 601 | }; 602 | 603 | int go(cmdline& c) { 604 | string s = c.first(); 605 | { 606 | auto i = verbs.find(s); 607 | if (i != verbs.end()) { 608 | i->second->init(c); 609 | i->second->go(); 610 | return 0; 611 | } 612 | } 613 | 614 | if (s == "head") { 615 | head h; 616 | int x = c.getNum("n"); 617 | if (x > 0) { 618 | h.N = x; 619 | } 620 | int sa = c.getNum("s"); 621 | if (sa > 0) { 622 | h.startAt = sa; 623 | } 624 | h.go(); 625 | } 626 | else if (s == "project") { 627 | project x; 628 | strSplit(x.fields, c.second()); 629 | x.go(); 630 | } 631 | else if (s == "del") { 632 | del x; 633 | x.fieldName = c.second(); 634 | x.go(); 635 | } 636 | else if (s == "promote") { 637 | promote x; 638 | x.fieldName = c.second(); 639 | x.go(); 640 | } 641 | else if (s == "merge") { 642 | string fn = c.second(); 643 | ifstream f(fn); 644 | if (!f.is_open()) { 645 | cerr << "error couldn't open file: " << fn << endl; 646 | exit(1); 647 | } 648 | merge(cin, f); 649 | } 650 | else if (s == "sample") { 651 | sample s; 652 | int x = c.getNum("n"); 653 | if (x < 1) 654 | throw bsontool_error("bad or missing -n argument"); 655 | s.N = x; 656 | s.go(); 657 | } 658 | else if (s == "tail") { 659 | tail h; 660 | int x = c.getNum("n"); 661 | if (x > 0) { 662 | h.N = x; 663 | } 664 | h.go(); 665 | } 666 | else if (s == "print") { 667 | if (emitDocNumber) { 668 | throw bsontool_error("can't use -# option with print command"); 669 | } 670 | print x; 671 | x.go(c); 672 | } 673 | else if (s == "text") { 674 | text t; 675 | t.go(); 676 | } 677 | else if (s == "grep") { 678 | grep t(c.second()); 679 | t.go(); 680 | } 681 | else if (s == "count") { 682 | count x; 683 | x.go(); 684 | cout << x.nDocs() << '\t' << x.largest << endl; 685 | } 686 | else { 687 | cout << "Error: bad command line option? Use -h for help." << endl; 688 | return -1; 689 | } 690 | return 0; 691 | } 692 | 693 | } 694 | 695 | const char QUOTE = '"'; 696 | const char COMMA = ','; 697 | 698 | void strSplit(vector &line, const string& str) { 699 | line.clear(); 700 | if (str.empty()) 701 | return; 702 | 703 | const char *p = str.c_str(); 704 | while (1) { 705 | StringBuilder s; 706 | bool quoted = (*p == QUOTE); 707 | if (quoted) { 708 | p++; 709 | } 710 | while (1) { 711 | if (*p == QUOTE) { 712 | if (p[1] == QUOTE) { 713 | s << QUOTE; 714 | p += 2; 715 | } 716 | else { 717 | // end of field 718 | uassert(1000, "unexpected quote", quoted); 719 | p++; 720 | break; 721 | } 722 | } 723 | else if (*p == COMMA && !quoted) { 724 | break; 725 | } 726 | else if (*p == 0) { 727 | if (!quoted) { 728 | break; 729 | } 730 | else { 731 | throw bsontool_error("expected quote"); 732 | } 733 | } 734 | else { 735 | s << *p++; 736 | } 737 | } 738 | line.push_back(s.str()); 739 | if (*p == COMMA) { 740 | p++; 741 | continue; 742 | } 743 | if (*p == 0) { 744 | break; 745 | } 746 | throw bsontool_error("error: expected comma or end of line (after quote?)"); 747 | } 748 | } 749 | 750 | bool parms(cmdline& c) { 751 | if (c.help()) { 752 | cout << "bson - utility for manipulating BSON files.\n\n"; 753 | cout << "BSON (\"Binary JSON\") is a binary representation of JSON documents.\n"; 754 | cout << "This program provides simple utility-type manipulations of BSON files.\n"; 755 | cout << "A BSON file (typically with .bson suffix) is simply a series of BSON \n"; 756 | cout << "documents appended contiguously in a file.\n\n"; 757 | cout << "The utility reads BSON documents from stdin. Thus you can do various \n"; 758 | cout << "piping-type operations from the command line if desired.\n"; 759 | cout << "\n"; 760 | cout << "Usage:\n"; 761 | cout << "\n"; 762 | cout << " bson \n"; 763 | cout << "\n"; 764 | cout << "Options:\n"; 765 | cout << " -h for help\n"; 766 | cout << " -# emit document number instead of the document's bson content\n"; 767 | cout << " -N emit null rather than nothing, when applicable (eg w/pull)\n"; 768 | cout << "\n"; 769 | cout << "Verbs:\n"; 770 | cout << " count output # of documents in file, then size in bytes of largest document\n"; 771 | cout << " head [-n ] [-s ] output the first N documents. (N=10 default)\n"; 772 | cout << " tail [-n ] note: current tail implementation is slow\n"; 773 | cout << " sample -n output every Nth document\n"; 774 | cout << " print [pretty] convert bson to json (print json to stdout)\n"; 775 | cout << " text extract text values and output (no fieldnames)\n"; 776 | cout << " grep text search each document for a simple text pattern. Not actual\n"; 777 | cout << " regular expressions yet. outputs matched documents. Element values\n"; 778 | cout << " are searched for, field names are not.\n"; 779 | cout << " project project comma separated list of top level fieldnames to output\n"; 780 | cout << " del delete a top level field and then re-output the stream\n"; 781 | cout << " promote pull out the subobject specified by fieldname and output it (only).\n"; 782 | cout << " if the field is missing or not an object nothing will be output. Dot\n"; 783 | cout << " notation is supported.\n"; 784 | cout << " merge merge bson stream stdin with bson file on cmd line. should be same\n"; 785 | cout << " number of documents in each input.\n"; 786 | for (auto i = bsontools::verbs.begin(); i != bsontools::verbs.end(); i++) { 787 | cout << " " << i->second->help() << '\n'; 788 | } 789 | cout << "\n"; 790 | return true; 791 | } 792 | bsontools::emitDocNumber = c.got("#"); 793 | bsontools::emitNull = c.got("N"); 794 | return false; 795 | } 796 | 797 | int main(int argc, char* argv[]) 798 | { 799 | try { 800 | cmdline c(argc, argv); 801 | if (parms(c)) 802 | return 0; 803 | 804 | // temp 805 | if( 0 ){ 806 | 807 | bsonobjbuilder b; 808 | bsonobj o = b.append("x", 3).append("Y", "abc").obj(); 809 | cout << o.toString() << endl; 810 | bsonelement e = o.getFieldDotted("y"); 811 | e.isObject(); 812 | } 813 | 814 | return bsontools::go(c); 815 | } 816 | catch (std::exception& e) { 817 | cerr << "bson util error: "; 818 | cerr << e.what() << endl; 819 | return EXIT_FAILURE; 820 | } 821 | return 0; 822 | } 823 | -------------------------------------------------------------------------------- /src/bsontools/cmdline.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include "../../../bson-cxx/src/bson/parse_number.h" 6 | 7 | using namespace _bson; 8 | using namespace std; 9 | 10 | class option { 11 | public: 12 | std::string opt; 13 | std::string value; 14 | int v; 15 | }; 16 | 17 | class cmdline { 18 | int c; 19 | char **v; 20 | public: 21 | std::vector