All Projects → tuananh → Camaro

tuananh / Camaro

Licence: mit
camaro is an utility to transform XML to JSON, using Node.js binding to native XML parser pugixml, one of the fastest XML parser around.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Camaro

Wasmer
🚀 The leading WebAssembly Runtime supporting WASI and Emscripten
Stars: ✭ 11,047 (+2422.15%)
Mutual labels:  emscripten, webassembly, wasm
Dcmjs
dcmjs is a javascript cross-compile of dcmtk (dcmtk.org).
Stars: ✭ 92 (-79%)
Mutual labels:  emscripten, webassembly, wasm
Emscripten Docker
Docker image with Emscripten to compile ASM.js and WebAssembly
Stars: ✭ 92 (-79%)
Mutual labels:  emscripten, webassembly, wasm
Wasmjit
Small Embeddable WebAssembly Runtime
Stars: ✭ 1,063 (+142.69%)
Mutual labels:  emscripten, webassembly, wasm
Rustynes
👾 An NES emulator by Rust and WebAssembly
Stars: ✭ 399 (-8.9%)
Mutual labels:  emscripten, webassembly, wasm
Opus Stream Decoder
Instantly decode Ogg Opus audio streams in chunks with JavaScript & WebAssembly (Wasm)
Stars: ✭ 80 (-81.74%)
Mutual labels:  emscripten, webassembly, wasm
Wac
WebAssembly interpreter in C
Stars: ✭ 372 (-15.07%)
Mutual labels:  emscripten, webassembly, wasm
Xml Js
Converter utility between XML text and Javascript object / JSON text.
Stars: ✭ 874 (+99.54%)
Mutual labels:  json, xml, xml-parser
TypeScriptXX
🧷 Stay safe! Type-safe scripting for C++ using TypeScriptToLua and CMake with auto-generated declarations.
Stars: ✭ 33 (-92.47%)
Mutual labels:  webassembly, wasm, emscripten
Webassembly Examples
From Simple To Complex. A complete collection of webassembly examples.
Stars: ✭ 177 (-59.59%)
Mutual labels:  emscripten, webassembly, wasm
Cppwasm Book
📚 WebAssembly friendly programming with C/C++ -- Emscripten practice
Stars: ✭ 956 (+118.26%)
Mutual labels:  emscripten, webassembly, wasm
Wasm Git
GIT for nodejs and the browser using https://libgit2.org compiled to WebAssembly with https://emscripten.org
Stars: ✭ 261 (-40.41%)
Mutual labels:  emscripten, webassembly, wasm
Omniparser
omniparser: a native Golang ETL streaming parser and transform library for CSV, JSON, XML, EDI, text, etc.
Stars: ✭ 148 (-66.21%)
Mutual labels:  json, xml, transform
Node Rest Client
REST API client from node.js
Stars: ✭ 365 (-16.67%)
Mutual labels:  json, xml, xml-parser
Internettools
XPath/XQuery 3.1 interpreter for Pascal with compatibility modes for XPath 2.0/XQuery 1.0/3.0, custom and JSONiq extensions, XML/HTML parsers and classes for HTTP/S requests
Stars: ✭ 82 (-81.28%)
Mutual labels:  json, xml, xpath
Assortedwidgets
OpenGL GUI library
Stars: ✭ 92 (-79%)
Mutual labels:  emscripten, webassembly, wasm
Sirix
SirixDB is a temporal, evolutionary database system, which uses an accumulate only approach. It keeps the full history of each resource. Every commit stores a space-efficient snapshot through structural sharing. It is log-structured and never overwrites data. SirixDB uses a novel page-level versioning approach called sliding snapshot.
Stars: ✭ 638 (+45.66%)
Mutual labels:  json, xml, xpath
Dasel
Query, update and convert data structures from the command line. Comparable to jq/yq but supports JSON, TOML, YAML, XML and CSV with zero runtime dependencies.
Stars: ✭ 759 (+73.29%)
Mutual labels:  json, xml, xml-parser
Modern Wasm Starter
🛸 Run C++ code on web and create blazingly fast websites! A starter template to easily create WebAssembly packages using type-safe C++ bindings with automatic TypeScript declarations.
Stars: ✭ 140 (-68.04%)
Mutual labels:  emscripten, webassembly, wasm
koder
QR/bar code scanner for the Browser
Stars: ✭ 73 (-83.33%)
Mutual labels:  webassembly, wasm, emscripten

camaro

camaro is an utility to transform XML to JSON, using Node.js binding to native XML parser pugixml, one of the fastest XML parser around.

npm npm bundle size Build status TypeScript definitions on DefinitelyTyped npm

🤘 Features

  • Transform XML to JSON.

    • Only take properties that you're interested in.
    • Output is a ready to use JS object.
    • For those that need a complete document parser, checkout my other project @tuananh/sax-parser - a pretty fast native module, XML-compliant SAX parser for Node.js.
  • Written in C++ and compiled down to WebAssembly so no re-compilation needed.

    • No need to build binary whenever a new Node version released.
    • Work on all major platforms (OS X, Linux and Windows). See Travis CI and AppVeyor build status for details.
    • AWS Lambda friendly (or serverless in general).
  • It's pretty fast on large XML strings.

    • We're using pugixml underneath. It's one of the fastest XML parser around.
    • Scale well with multi-core processor by use of worker_threads pool (Node >= 12).
  • Pretty print XML.

🔥 Benchmark

300 KB XML file 100 KB XML file
60 KB XML file 7 KB XML file

XML file is an actual XML response from Expedia API. I just delete some nodes to change its size for benchmarking.

For complete benchmark, see benchmark/index.md.

  • Please note that this is an unfair game for camaro because it only transform those fields specified in template. The whole reason of me creating this is because most of the time, I'm just interested in some of the data in the whole XML mess.
  • I may expose another method to transform the whole XML tree so that the benchmark will better reflect the real performance.
  • 🚧 Performance on small XML strings will probably be worse than pure JavaScript implementation. If your use cases consist of small XML strings only, you probably don't need this.
  • Some other libraries that I used to use for benchmark like rapidx2j or xml2json no longer works on Node 14 so I remove them from the benchmark.

intro

Installation

yarn add camaro
# npm install camaro

Usage

You can use our custom template format powered by XPath.

We also introduce some custom syntax such as:

  • if a path start with #, that means it's a constant. E.g: #1234 will return 1234
  • if a path is empty, return blank
  • Some string manipulation functions which are not availble in XPath 1.0 such as lower-case, upper-case, title-case, camel-case, snake-case, string-join or raw. Eventually, I'm hoping to add all XPath 2.0 functions but these are all that I need for now. PRs welcome.

The rest are pretty much vanilla XPath 1.0.

For complete API documentation, please see API.md

Additional examples can be found in the examples folder at https://github.com/tuananh/camaro/tree/develop/examples.

const { transform, prettyPrint } = require('camaro')

const xml = `
    <players>
        <player jerseyNumber="10">
            <name>wayne rooney</name>
            <isRetired>false</isRetired>
            <yearOfBirth>1985</yearOfBirth>
        </player>
        <player jerseyNumber="7">
            <name>cristiano ronaldo</name>
            <isRetired>false</isRetired>
            <yearOfBirth>1985</yearOfBirth>
        </player>
        <player jerseyNumber="7">
            <name>eric cantona</name>
            <isRetired>true</isRetired>
            <yearOfBirth>1966</yearOfBirth>
        </player>
    </players>
`

/**
 * the template can be an object or an array depends on what output you want the XML to be transformed to.
 * 
 * ['players/player', {name, ...}] means that: Get all the nodes with this XPath expression `players/player`.
 *      - the first param is the XPath path to get all the XML nodes.
 *      - the second param is a string or an object that describe the shape of the array element and how to get it.
 * 
 * For each of those XML node
 *      - call the XPath function `title-case` on field `name` and assign it to `name` field of the output.
 *      - get the attribute `jerseyNumber` from XML node player
 *      - get the `yearOfBirth` attribute from `yearOfBirth` and cast it to number.
 *      - cast `isRetired` to true if its string value equals to "true", and false otherwise.
 */

const template = ['players/player', {
    name: 'title-case(name)',
    jerseyNumber: '@jerseyNumber',
    yearOfBirth: 'number(yearOfBirth)',
    isRetired: 'boolean(isRetired = "true")'
}]

;(async function () {
    const result = await transform(xml, template)
    console.log(result)

    const prettyStr = await prettyPrint(xml, { indentSize: 4})
    console.log(prettyStr)
})()

Output of transform()

[
    {
        name: 'Wayne Rooney',
        jerseyNumber: 10,
        yearOfBirth: 1985,
        isRetired: false,
    },
    {
        name: 'Cristiano Ronaldo',
        jerseyNumber: 7,
        yearOfBirth: 1985,
        isRetired: false,
    },
    {
        name: 'Eric Cantona',
        jerseyNumber: 7,
        yearOfBirth: 1966,
        isRetired: true,
    }
]

And output of prettyPrint()

<players>
    <player jerseyNumber="10">
        <name>Wayne Rooney</name>
        <isRetired>false</isRetired>
        <yearOfBirth>1985</yearOfBirth>
    </player>
    <player jerseyNumber="7">
        <name>Cristiano Ronaldo</name>
        <isRetired>false</isRetired>
        <yearOfBirth>1985</yearOfBirth>
    </player>
    <player jerseyNumber="7">
        <name>Eric Cantona</name>
        <isRetired>true</isRetired>
        <yearOfBirth>1966</yearOfBirth>
    </player>
</players>

Similar projects

  • cruftless: I personally find this project very fascinating. Its template engine is more powerful than camaro's XPath-based perhaps. You should check it out.

Used by

...

Licence

The MIT License

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].