|
1 | | -Convert Java `.properties` files to JSON (using JavaScript). |
| 1 | +Convert Java `.properties` files to JSON (using JavaScript/TypeScript). |
2 | 2 |
|
3 | | -supports array creation. |
| 3 | +Supports nested objects, arrays, Unicode escape sequences, and type coercion. |
4 | 4 |
|
5 | | -The function `propertiesToJSON` takes a string and returns |
6 | | -a JavaScript object. |
| 5 | +## Install |
7 | 6 |
|
8 | | -### use |
| 7 | +```sh |
| 8 | +# pnpm |
| 9 | +pnpm add js-properties-to-json |
9 | 10 |
|
| 11 | +# npm |
| 12 | +npm i js-properties-to-json |
10 | 13 | ``` |
11 | | - propertiesToJSON(data, { |
12 | | - jsonAsString: false, |
13 | | - convertToJsonTree : false, |
14 | | - parseNumber:false, |
15 | | - parseBooleanNullUndefined:false, |
16 | | - }); |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +```ts |
| 18 | +import propertiesToJSON from 'js-properties-to-json'; |
| 19 | +// or CommonJS: |
| 20 | +// const propertiesToJSON = require('js-properties-to-json'); |
| 21 | + |
| 22 | +const result = propertiesToJSON(` |
| 23 | +# comment |
| 24 | +server.host=localhost |
| 25 | +server.port=8080 |
| 26 | +`); |
| 27 | +// { 'server.host': 'localhost', 'server.port': '8080' } |
17 | 28 | ``` |
18 | 29 |
|
19 | | -### Read a local file in `node`: |
| 30 | +### Read a local file in Node.js |
20 | 31 |
|
21 | 32 | ```js |
22 | | -const fs = require('fs'); |
23 | | -const path = require('path'); |
24 | | -const filePath = path.join(__dirname, 'sample.properties'); |
25 | | -const propertiesToJSON = require('properties-to-json');; |
26 | | - |
27 | | -fs.readFile(filePath, { encoding: 'utf-8' }, (err, data) => { |
28 | | - if (!err) { |
29 | | - console.log(propertiesToJSON(data)); |
30 | | - } |
31 | | -}); |
| 33 | +import fs from 'fs'; |
| 34 | +import propertiesToJSON from 'js-properties-to-json'; |
| 35 | + |
| 36 | +const data = fs.readFileSync('app.properties', 'utf-8'); |
| 37 | +console.log(propertiesToJSON(data, { convertToJsonTree: true, parseNumber: true })); |
32 | 38 | ``` |
33 | 39 |
|
34 | | -### Read a remote file in the browser: |
| 40 | +### Read a remote file in the browser |
35 | 41 |
|
36 | 42 | ```js |
37 | 43 | import propertiesToJSON from 'js-properties-to-json'; |
38 | 44 |
|
39 | | -const propsFile = new Request('<link-to-properties-file>'); |
40 | | - |
41 | | -const props = fetch(propsFile) |
42 | | - .then((response) => { |
43 | | - return response.text(); |
44 | | - }) |
45 | | - .then((text) => { |
46 | | - const propsText = propertiesToJSON(text); |
47 | | - console.log(propsText); |
48 | | - return propsText; |
49 | | - }); |
| 45 | +const text = await fetch('/app.properties').then((r) => r.text()); |
| 46 | +console.log(propertiesToJSON(text)); |
50 | 47 | ``` |
51 | 48 |
|
52 | | -### Available options |
| 49 | +## Options |
| 50 | + |
| 51 | +```ts |
| 52 | +import propertiesToJSON, { type Options } from 'js-properties-to-json'; |
| 53 | +``` |
| 54 | + |
| 55 | +| Option | Default | Description | |
| 56 | +| :-------------------------: | :-----: | :------------------------------------------------------------------------------------------: | |
| 57 | +| `jsonAsString` | `false` | Return the result as a JSON string instead of an object | |
| 58 | +| `convertToJsonTree` | `false` | Convert dotted keys into nested objects — e.g. `a.b=c` → `{ a: { b: "c" } }` | |
| 59 | +| `parseNumber` | `false` | Coerce numeric strings to numbers — e.g. `port=8080` → `{ port: 8080 }` | |
| 60 | +| `parseBooleanNullUndefined` | `false` | Coerce `"true"`, `"false"`, `"null"`, `"undefined"` to their JS equivalents (case-sensitive) | |
| 61 | + |
| 62 | +## Examples |
53 | 63 |
|
54 | | -| Option | Default value | Description | |
55 | | -| :-----------------------: | :----------------: | :------------------------------------------------------------------------------------------: | |
56 | | -| jsonAsString | false | return json as a string | |
57 | | -| convertToJsonTree | false | convert properties to json tree eg `a.b=c` to `{ "a.b": c }` if false or `{ "a": {"b": c} }` | |
58 | | -| parseNumber | false | parse value to number e.g - `a=1` to `{ a: "1" }` or `{ a: 1 }` | |
59 | | -| parseBooleanNullUndefined | false | parse string value of `null`, `true`, `false`, `undefined` | |
| 64 | +| Properties | JSON | |
| 65 | +| ------------------ | ---------------------------------- | |
| 66 | +| `a=b` | `{ "a": "b" }` | |
| 67 | +| `a:b` | `{ "a": "b" }` | |
| 68 | +| `a = b` | `{ "a": "b" }` | |
| 69 | +| `a[0]=x` | `{ "a": ["x"] }` | |
| 70 | +| `a[1][1]=b` | `{ "a": [null, [null, "b"]] }` | |
| 71 | +| `a.b.c=d` | `{ "a": { "b": { "c": "d" } } }` (requires `convertToJsonTree`) | |
| 72 | +| `k=\u00e9` | `{ "k": "é" }` | |
| 73 | +| `k=hello\tworld` | `{ "k": "hello\tworld" }` | |
60 | 74 |
|
61 | | -### Examples |
| 75 | +## Supported escape sequences |
62 | 76 |
|
63 | | -| Properties | JSON | |
64 | | -| ----------- | ---------------------------------- | |
65 | | -| `a=b` | `{ "a": "b" }` | |
66 | | -| `a[1][1]=b` | `{ "a": [ null, [ null, "b" ] ] }` | |
| 77 | +| Sequence | Result | |
| 78 | +| ------------- | ----------------------- | |
| 79 | +| `\uXXXX` | Unicode character (hex) | |
| 80 | +| `\t` | Tab | |
| 81 | +| `\n` | Newline | |
| 82 | +| `\r` | Carriage return | |
| 83 | +| `\f` | Form feed | |
| 84 | +| `\\` | Literal backslash | |
| 85 | +| `\=`, `\:` | Literal `=` or `:` in a key | |
67 | 86 |
|
68 | | -### How do I get it? |
| 87 | +## Limitations |
69 | 88 |
|
70 | | -1. `npm i js-properties-to-json` |
| 89 | +- Whitespace-only key separators (e.g. `key value`) are not supported — use `=` or `:`. |
| 90 | +- Inline comments (e.g. `key=value # comment`) are not supported; the `# comment` becomes part of the value. |
| 91 | +- `parseBooleanNullUndefined` is case-sensitive: `"True"` stays as a string. |
71 | 92 |
|
72 | | -### online converter |
| 93 | +## Online converter |
73 | 94 |
|
74 | | -[link](https://mehimanshupatil.github.io/propertiesToJSON/) |
| 95 | +[https://mehimanshupatil.github.io/propertiesToJSON/](https://mehimanshupatil.github.io/propertiesToJSON/) |
75 | 96 |
|
76 | | -### inspiration |
| 97 | +## Inspiration |
77 | 98 |
|
78 | | -[https://github.com/ryanpcmcquen/propertiesToJSON](https://github.com/ryanpcmcquen/propertiesToJSON) |
79 | | -[https://github.com/jeanpaulattard/json-to-properties](https://github.com/jeanpaulattard/json-to-properties) |
| 99 | +- [ryanpcmcquen/propertiesToJSON](https://github.com/ryanpcmcquen/propertiesToJSON) |
| 100 | +- [jeanpaulattard/json-to-properties](https://github.com/jeanpaulattard/json-to-properties) |
0 commit comments