├── .github └── workflows │ └── test.yml ├── .gitignore ├── .prettierrc ├── LICENSE.txt ├── README.ja.md ├── README.md ├── index.d.ts ├── index.js ├── package-lock.json ├── package.json └── test ├── index.test.js ├── test1.json └── test1.result /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: 'coverage' 2 | on: 3 | pull_request: 4 | branches: 5 | - master 6 | jobs: 7 | coverage: 8 | runs-on: ubuntu-latest 9 | steps: 10 | - uses: actions/checkout@v1 11 | - uses: ArtiomTr/jest-coverage-report-action@v2 -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/ 2 | node_modules/ -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | {} 2 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright 2022 rhenium_nv 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.ja.md: -------------------------------------------------------------------------------- 1 | [English](README.md) | [日本語](README.ja.md) 2 | 3 | # json2emap 4 | 5 | Json を Emap 文字列に変換します。 6 | Emap は [Neos Metaverse](https://neos.com/) でパースしやすいように考えて作ったデータ形式です。 7 | 8 | ## 使い方 9 | 10 | ``` 11 | npm install json2emap 12 | ``` 13 | 14 | sample..s 15 | 16 | ``` 17 | const json2emap = require("json2emap"); 18 | 19 | console.log(json2emap([1, 2, 3])); 20 | 21 | console.log(json2emap({ a: 123, b: "Hello", c: "World" })); 22 | 23 | console.log( 24 | json2emap({ 25 | a: ["Hello", "World"], 26 | b: [{ c: 1, d: 2 }], 27 | }) 28 | ); 29 | ``` 30 | 31 | Output 32 | 33 | ``` 34 | l$#4$#v$#k0$#length$#v0$#3$#t0$#number$#k1$#_0_$#v1$#1$#t1$#number$#k2$#_1_$#v2$#2$#t2$#number$#k3$#_2_$#v3$#3$#t3$#number$# 35 | l$#3$#k0$#a$#v0$#123$#t0$#number$#k1$#b$#v1$#Hello$#t1$#string$#k2$#c$#v2$#World$#t2$#string$# 36 | l$#6$#k0$#a.length$#v0$#2$#t0$#number$#k1$#a_0_$#v1$#Hello$#t1$#string$#k2$#a_1_$#v2$#World$#t2$#string$#k3$#b.length$#v3$#1$#t3$#number$#k4$#b_0_.c$#v4$#1$#t4$#number$#k5$#b_0_.d$#v5$#2$#t5$#number$# 37 | ``` 38 | 39 | ## Neos での Emap の使い方 40 | 41 | Emap 文字列の Neos 内での利用方法は主に 2 種類あります。 42 | DynamicVariable を利用するほうが機械的に処理できるためおすすめです。 43 | 44 | - DynamicVariable を利用する 45 | - 文字列から直接取り出す 46 | 47 | パブリックフォルダにサンプルが置いてあります。 48 | (以下のリンクを Neos 内でペーストするとパブリックフォルダとして出てきます。) 49 | 50 | > neosrec:///G-Shared-Project-rheni/R-166bcbdf-331a-4abc-9f27-8604bbf0de47 51 | 52 | ### DynamicVariable を利用する 53 | 54 | DynamicVariable への書き込み 55 | 56 | ![](https://user-images.githubusercontent.com/71165146/154020472-0fe7b6f9-b11b-4a4e-969d-84f5943b1747.jpg) 57 | 58 | DynamicVariable からの読み込み 59 | 60 | ![](https://user-images.githubusercontent.com/71165146/154020790-ff6c175a-3e4c-4525-8807-57138b891b13.jpg) 61 | 62 | ### 文字列から直接取り出す 63 | 64 | キーを元に特定の値とその型を取得できます。 65 | 66 | ![image](https://user-images.githubusercontent.com/71165146/155488876-96c1a261-b3c8-48f4-b50f-91d36132c11d.png) 67 | 68 | ※0.2.0 よりも古いバージョンの Emap 文字列では Key と Value の順番が違うためこの方法では読み込めません。 69 | 70 | ## Json から Emap に変換される手順 71 | 72 | 以下の JSON を変換してみます。 73 | 74 | ``` 75 | { 76 | "id": 123, 77 | "name": "rhenium", 78 | "isPublic": true, 79 | "accounts": [ 80 | { 81 | "type": "github", 82 | "link": "https://github.com/rheniumNV" 83 | }, 84 | { 85 | "type": "twitter", 86 | "link": "https://twitter.com/rhenium_nv" 87 | } 88 | ] 89 | } 90 | ``` 91 | 92 | 全てのパスを列挙します。 93 | リストには length を追加し、それぞれのパスは hoge[0] ではなく hoge_0\_ と表現します。 94 | (Neos の DynamicVariable のキーには[]が使えないためです。) 95 | 96 | ``` 97 | [ 98 | "id": 123, 99 | "name": "rhenium", 100 | "isPublic": true, 101 | "accounts.length": 2, 102 | "accounts_0_.type": "github", 103 | "accounts_0_.link": "https://github.com/rheniumNV" 104 | "accounts_1_.type", "twitter", 105 | "accounts_1_.link", "https://twitter.com/rhenium_nv", 106 | ] 107 | ``` 108 | 109 | それぞれのパスに対応する値とその型を追加します。 110 | 111 | ``` 112 | [ 113 | { 114 | "key": "id", 115 | "value": 123, 116 | "type": "number" 117 | }, 118 | { 119 | "key": "name" 120 | "value": "rhenium", 121 | "type": "string" 122 | }, 123 | { 124 | "key": "isPublic", 125 | "value": true, 126 | "type": "bool" 127 | }, 128 | { 129 | "key": "accounts.length", 130 | "value": 2, 131 | "type": "number" 132 | }, 133 | { 134 | "key": "accounts_0_.type", 135 | "value": "github", 136 | "type": "string" 137 | }, 138 | { 139 | "key": "accounts_0_.link", 140 | "value": "https://github.com/rheniumNV", 141 | "type": "string" 142 | }, 143 | { 144 | "key": "accounts_1_.type", 145 | "value": "twitter", 146 | "type": "string" 147 | }, 148 | { 149 | "key": "accounts_1_.link", 150 | "value": "https://twitter.com/rhenium_nv", 151 | "type": "string" 152 | ] 153 | ``` 154 | 155 | フラットにします。 156 | 各キーは 1 文字に省略しインデックスを追加します。 157 | また、全体の長さを l に入れます。 158 | 159 | ``` 160 | key->k 161 | value=->v 162 | type->t 163 | ``` 164 | 165 | ``` 166 | { 167 | "l": 8, 168 | 169 | "k0": "id", 170 | "v0": "123", 171 | "t0": "number", 172 | 173 | "k1": "name", 174 | "v1": "rhenium", 175 | "t1": "string", 176 | 177 | "k2": "isPublic", 178 | "v2": "true", 179 | "t2": "bool", 180 | 181 | "k3": "accounts.length", 182 | "v3": "2", 183 | "t3": "number", 184 | 185 | "k4": "accounts_0_.type", 186 | "v4": "github", 187 | "t4": "string", 188 | 189 | "k5": "accounts_0_.link", 190 | "v5": "https://github.com/rheniumNV", 191 | "t5": "string", 192 | 193 | "k6": "accounts_1_.type", 194 | "v6": "twitter", 195 | "t6": "string", 196 | 197 | "k7": "accounts_1_.link", 198 | "v7": "https://twitter.com/rhenium_nv", 199 | "t7": "string", 200 | } 201 | ``` 202 | 203 | 文字列にします。 204 | 205 | "と{}は取り除きます。 206 | :と,は$#に置換します。 207 | 208 | l とそれ以降の文字の間に v$#を入れます。 209 | (これは古いバージョンの Emap を処理するロジックとの互換性を保つためのものです) 210 | 211 | この際に\\\\や$#が key か value に出てきた場合は以下のように置換します。 212 | 213 | ``` 214 | \ -> \\ 215 | $# -> $\# 216 | ``` 217 | 218 | ``` 219 | l $# 8 $# 220 | 221 | v$# 222 | 223 | k0 $# id $# 224 | v0 $# 123 $# 225 | t0 $# number $# 226 | 227 | k1 $# name $# 228 | v1 $# rhenium $# 229 | t1 $# string $# 230 | 231 | k2 $# isPublic $# 232 | v2 $# true $# 233 | t2 $# bool $# 234 | 235 | k3 $# accounts.length $# 236 | v3 $# 2 $# 237 | t3 $# number $# 238 | 239 | k4 $# accounts_0_.type $# 240 | v4 $# github $# 241 | t4 $# string $# 242 | 243 | k5 $# accounts_0_.link $# 244 | v5 $# https://github.com/rheniumNV $# 245 | t5 $# string $# 246 | 247 | k6 $# accounts_1_.type $# 248 | v6 $# twitter $# 249 | t6 $# string $# 250 | 251 | k7 $# accounts_1_.link $# 252 | v7 $# https://twitter.com/rhenium_nv $# 253 | t7 $# string $# 254 | ``` 255 | 256 | 実際は空白や改行はないので以下のようになります。 257 | 258 | ``` 259 | l$#8$#v$#k0$#id$#v0$#123$#t0$#number$#k1$#name$#v1$#rhenium$#t1$#string$#k2$#isPublic$#v2$#true$#t2$#bool$#k3$#accounts.length$#v3$#2$#t3$#number$#k4$#accounts_0_.type$#v4$#github$#t4$#string$#k5$#accounts_0_.link$#v5$#https://github.com/rheniumNV$#t5$#string$#k6$#accounts_1_.type$#v6$#twitter$#t6$#string$#k7$#accounts_1_.link$#v7$#https://twitter.com/rhenium_nv$#t7$#string$# 260 | ``` 261 | 262 | これで完成です。 263 | 264 | ### エスケープの例 265 | 266 | ``` 267 | { 268 | "k0": "\test", 269 | "v0": "$#value" 270 | } 271 | -> k0$#\\test$#v0$#$\#value$# 272 | ``` 273 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [English](README.md) | [日本語](README.ja.md) 2 | 3 | # json2emap 4 | 5 | Convert Json to Emap string. 6 | Emap is a data format designed to be easily parsed by [Neos Metaverse](https://neos.com/). 7 | 8 | ## How to use 9 | 10 | ``` 11 | npm install json2emap 12 | ``` 13 | 14 | sample..s 15 | 16 | ``` 17 | const json2emap = require("json2emap"); 18 | 19 | console.log(json2emap([1, 2, 3])); 20 | 21 | console.log(json2emap({ a: 123, b: "Hello", c: "World" })); 22 | 23 | console.log( 24 | json2emap({ 25 | a: ["Hello", "World"], 26 | b: [{ c: 1, d: 2 }], 27 | }) 28 | ); 29 | ``` 30 | 31 | Output 32 | 33 | ``` 34 | l$#4$#v$#k0$#length$#v0$#3$#t0$#number$#k1$#_0_$#v1$#1$#t1$#number$#k2$#_1_$#v2$#2$#t2$#number$#k3$#_2_$#v3$#3$#t3$#number$# 35 | l$#3$#k0$#a$#v0$#123$#t0$#number$#k1$#b$#v1$#Hello$#t1$#string$#k2$#c$#v2$#World$#t2$#string$# 36 | l$#6$#k0$#a.length$#v0$#2$#t0$#number$#k1$#a_0_$#v1$#Hello$#t1$#string$#k2$#a_1_$#v2$#World$#t2$#string$#k3$#b.length$#v3$#1$#t3$#number$#k4$#b_0_.c$#v4$#1$#t4$#number$#k5$#b_0_.d$#v5$#2$#t5$#number$# 37 | ``` 38 | 39 | ## How to use Emap in Neos 40 | 41 | There are two main ways to use Emap strings in Neos. 42 | Using DynamicVariable is recommended because it can be handled mechanically. 43 | 44 | - Using DynamicVariable 45 | - Extracting directly from strings 46 | 47 | You can find a sample in the public folder. 48 | (You can paste the following link in Neos, and it will appear as a public folder.) 49 | 50 | > neosrec:///G-Shared-Project-rheni/R-166bcbdf-331a-4abc-9f27-8604bbf0de47 51 | 52 | ### Using DynamicVariable 53 | 54 | Write to DynamicVariable 55 | 56 | ![](https://user-images.githubusercontent.com/71165146/154020472-0fe7b6f9-b11b-4a4e-969d-84f5943b1747.jpg) 57 | 58 | Read from DynamicVariable 59 | 60 | ![](https://user-images.githubusercontent.com/71165146/154020790-ff6c175a-3e4c-4525-8807-57138b891b13.jpg) 61 | 62 | ### Extracting directly from strings 63 | 64 | You can retrieve a specific value and its type based on a key. 65 | 66 | ![image](https://user-images.githubusercontent.com/71165146/155488876-96c1a261-b3c8-48f4-b50f-91d36132c11d.png) 67 | 68 | ※0.2.0 よりも古いバージョンの Emap 文字列では Key と Value の順番が違うためこの方法では読み込めません。 69 | 70 | ## Steps to convert from Json to Emap 71 | 72 | Let's convert the following JSON. 73 | 74 | ``` 75 | { 76 | "id": 123, 77 | "name": "rhenium", 78 | "isPublic": true, 79 | "accounts": [ 80 | { 81 | "type": "github", 82 | "link": "https://github.com/rheniumNV" 83 | }, 84 | { 85 | "type": "twitter", 86 | "link": "https://twitter.com/rhenium_nv" 87 | } 88 | ] 89 | } 90 | ``` 91 | 92 | Enumerate all paths. 93 | We add length to the list, and each path is represented as hoge_0\_ instead of hoge[0] (because Neos DynamicVariable keys cannot use []). 94 | (This is because Neos DynamicVariable does not allow [] as a key.) 95 | 96 | ``` 97 | [ 98 | "id": 123, 99 | "name": "rhenium", 100 | "isPublic": true, 101 | "accounts.length": 2, 102 | "accounts_0_.type": "github", 103 | "accounts_0_.link": "https://github.com/rheniumNV" 104 | "accounts_1_.type", "twitter", 105 | "accounts_1_.link", "https://twitter.com/rhenium_nv", 106 | ] 107 | ``` 108 | 109 | For each path, add the corresponding value and its type. 110 | 111 | ``` 112 | [ 113 | { 114 | "key": "id", 115 | "value": 123, 116 | "type": "number" 117 | }, 118 | { 119 | "key": "name" 120 | "value": "rhenium", 121 | "type": "string" 122 | }, 123 | { 124 | "key": "isPublic", 125 | "value": true, 126 | "type": "bool" 127 | }, 128 | { 129 | "key": "accounts.length", 130 | "value": 2, 131 | "type": "number" 132 | }, 133 | { 134 | "key": "accounts_0_.type", 135 | "value": "github", 136 | "type": "string" 137 | }, 138 | { 139 | "key": "accounts_0_.link", 140 | "value": "https://github.com/rheniumNV", 141 | "type": "string" 142 | }, 143 | { 144 | "key": "accounts_1_.type", 145 | "value": "twitter", 146 | "type": "string" 147 | }, 148 | { 149 | "key": "accounts_1_.link", 150 | "value": "https://twitter.com/rhenium_nv", 151 | "type": "string" 152 | ] 153 | ``` 154 | 155 | Make it flat. 156 | Each key is abbreviated to a single character and an index is added. 157 | Also, put the total length in l. 158 | 159 | ``` 160 | key->k 161 | value=->v 162 | type->t 163 | ``` 164 | 165 | ``` 166 | { 167 | "l": 8, 168 | 169 | "k0": "id", 170 | "v0": "123", 171 | "t0": "number", 172 | 173 | "k1": "name", 174 | "v1": "rhenium", 175 | "t1": "string", 176 | 177 | "k2": "isPublic", 178 | "v2": "true", 179 | "t2": "bool", 180 | 181 | "k3": "accounts.length", 182 | "v3": "2", 183 | "t3": "number", 184 | 185 | "k4": "accounts_0_.type", 186 | "v4": "github", 187 | "t4": "string", 188 | 189 | "k5": "accounts_0_.link", 190 | "v5": "https://github.com/rheniumNV", 191 | "t5": "string", 192 | 193 | "k6": "accounts_1_.type", 194 | "v6": "twitter", 195 | "t6": "string", 196 | 197 | "k7": "accounts_1_.link", 198 | "v7": "https://twitter.com/rhenium_nv", 199 | "t7": "string", 200 | } 201 | ``` 202 | 203 | Make it a string. 204 | 205 | Remove " and {}. 206 | Replace :and , with $#. 207 | 208 | Put v$# between l and the next character. 209 | (This is to maintain compatibility with the logic that handles older versions of Emap.) 210 | 211 | In this case, if \\\\ or $# appears in key or value, replace it as follows 212 | 213 | ``` 214 | \ -> \\ 215 | $# -> $\# 216 | ``` 217 | 218 | ``` 219 | l $# 8 $# 220 | 221 | v$# 222 | 223 | k0 $# id $# 224 | v0 $# 123 $# 225 | t0 $# number $# 226 | 227 | k1 $# name $# 228 | v1 $# rhenium $# 229 | t1 $# string $# 230 | 231 | k2 $# isPublic $# 232 | v2 $# true $# 233 | t2 $# bool $# 234 | 235 | k3 $# accounts.length $# 236 | v3 $# 2 $# 237 | t3 $# number $# 238 | 239 | k4 $# accounts_0_.type $# 240 | v4 $# github $# 241 | t4 $# string $# 242 | 243 | k5 $# accounts_0_.link $# 244 | v5 $# https://github.com/rheniumNV $# 245 | t5 $# string $# 246 | 247 | k6 $# accounts_1_.type $# 248 | v6 $# twitter $# 249 | t6 $# string $# 250 | 251 | k7 $# accounts_1_.link $# 252 | v7 $# https://twitter.com/rhenium_nv $# 253 | t7 $# string $# 254 | ``` 255 | 256 | In fact, there are no spaces or line breaks, so it looks like this 257 | 258 | ``` 259 | l$#8$#v$#k0$#id$#v0$#123$#t0$#number$#k1$#name$#v1$#rhenium$#t1$#string$#k2$#isPublic$#v2$#true$#t2$#bool$#k3$#accounts.length$#v3$#2$#t3$#number$#k4$#accounts_0_.type$#v4$#github$#t4$#string$#k5$#accounts_0_.link$#v5$#https://github.com/rheniumNV$#t5$#string$#k6$#accounts_1_.type$#v6$#twitter$#t6$#string$#k7$#accounts_1_.link$#v7$#https://twitter.com/rhenium_nv$#t7$#string$# 260 | ``` 261 | 262 | It is now complete. 263 | 264 | ### Example of escaping 265 | 266 | ``` 267 | { 268 | "k0": "\test", 269 | "v0": "$#value" 270 | } 271 | -> k0$#\\test$#v0$#$\#value$# 272 | ``` 273 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | interface IOption { 2 | resolveTypeFunc?: Function; 3 | } 4 | declare function json2emap(json: any, option?: IOption): string; 5 | export = json2emap; 6 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const _ = require("lodash"); 2 | 3 | const isArray = (json) => Array.isArray(json); 4 | const isMap = (json) => _.isPlainObject(json); 5 | 6 | const resolveKey = (prefix, key) => (prefix ? `${prefix}.${key}` : key); 7 | 8 | const resolveArrayKey = (prefix, key) => 9 | prefix ? `${prefix}_${key}_` : `_${key}_`; 10 | 11 | const resolveIterator = (func, resolveKeyFunc, json, prefix) => 12 | _.flatMap(json, (value, key) => func(value, resolveKeyFunc(prefix, key))); 13 | 14 | const resolveType = (value) => 15 | _.isNumber(value) 16 | ? "number" 17 | : _.isString(value) 18 | ? "string" 19 | : _.isBoolean(value) 20 | ? "bool" 21 | : "any"; 22 | 23 | const resolveValue = ( 24 | value, 25 | key, 26 | overRideType, 27 | resolveTypeFunc = resolveType 28 | ) => ({ 29 | v: value === null ? "null" : value, 30 | k: key, 31 | t: overRideType ? overRideType : resolveTypeFunc(value), 32 | }); 33 | 34 | const resolveMap = (json, key = "", resolveTypeFunc) => { 35 | return isMap(json) 36 | ? resolveIterator(resolveMap, resolveKey, json, key) 37 | : isArray(json) 38 | ? [ 39 | resolveValue( 40 | Object.keys(json).length, 41 | resolveKey(key, "length"), 42 | "number", 43 | resolveTypeFunc 44 | ), 45 | ...resolveIterator(resolveMap, resolveArrayKey, json, key), 46 | ] 47 | : resolveValue(json, key, resolveTypeFunc); 48 | }; 49 | 50 | const escapeValue = (value) => 51 | _.replace(_.replace(value, /\\/g, "\\\\"), /\$#/g, "$\\#"); 52 | 53 | module.exports = (json, { resolveTypeFunc = resolveType } = {}) => { 54 | const list = resolveMap(json, undefined, resolveTypeFunc); 55 | let result = `l$#${Object.keys(list).length}$#v$#`; 56 | list.forEach(({ v, k, t }, index) => { 57 | result += `k${index}$#${escapeValue(k)}$#v${index}$#${escapeValue( 58 | v 59 | )}$#t${index}$#${escapeValue(t)}$#`; 60 | }); 61 | return result; 62 | }; 63 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "json2emap", 3 | "version": "0.1.2", 4 | "description": "Convert JSON to Emap, a format that is easy to parse in NeosVR.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "jest", 8 | "fix:format": "prettier --write **/*.{js,json,css}" 9 | }, 10 | "dependencies": { 11 | "lodash": "^4.17.21" 12 | }, 13 | "devDependencies": { 14 | "prettier": "^2.3.2", 15 | "jest": "^27.5.1" 16 | }, 17 | "repository": { 18 | "type": "git", 19 | "url": "git+https://github.com/rheniumNV/json2emap.git" 20 | }, 21 | "keywords": [ 22 | "neos", 23 | "neosvr", 24 | "emap" 25 | ], 26 | "author": "rhenium_nv", 27 | "license": "MIT" 28 | } 29 | -------------------------------------------------------------------------------- /test/index.test.js: -------------------------------------------------------------------------------- 1 | const json2emap = require("../index"); 2 | const fs = require("fs"); 3 | 4 | test("J2E", () => { 5 | expect( 6 | json2emap({ 7 | id: 123, 8 | name: "rhenium", 9 | isPublic: true, 10 | accounts: [ 11 | { 12 | type: "github", 13 | link: "https://github.com/rheniumNV", 14 | }, 15 | { 16 | type: "twitter", 17 | link: "https://twitter.com/rhenium_nv", 18 | }, 19 | ], 20 | }) 21 | ).toEqual( 22 | "l$#8$#v$#k0$#id$#v0$#123$#t0$#number$#k1$#name$#v1$#rhenium$#t1$#string$#k2$#isPublic$#v2$#true$#t2$#bool$#k3$#accounts.length$#v3$#2$#t3$#number$#k4$#accounts_0_.type$#v4$#github$#t4$#string$#k5$#accounts_0_.link$#v5$#https://github.com/rheniumNV$#t5$#string$#k6$#accounts_1_.type$#v6$#twitter$#t6$#string$#k7$#accounts_1_.link$#v7$#https://twitter.com/rhenium_nv$#t7$#string$#" 23 | ); 24 | }); 25 | 26 | test("J2E-copy", () => { 27 | expect( 28 | json2emap({ 29 | id: 123, 30 | name: "rhenium", 31 | isPublic: true, 32 | accounts: [ 33 | { 34 | type: "github", 35 | link: "https://github.com/rheniumNV", 36 | }, 37 | { 38 | type: "twitter", 39 | link: "https://twitter.com/rhenium_nv", 40 | }, 41 | ], 42 | }) 43 | ).toEqual( 44 | "l$#8$#v$#k0$#id$#v0$#123$#t0$#number$#k1$#name$#v1$#rhenium$#t1$#string$#k2$#isPublic$#v2$#true$#t2$#bool$#k3$#accounts.length$#v3$#2$#t3$#number$#k4$#accounts_0_.type$#v4$#github$#t4$#string$#k5$#accounts_0_.link$#v5$#https://github.com/rheniumNV$#t5$#string$#k6$#accounts_1_.type$#v6$#twitter$#t6$#string$#k7$#accounts_1_.link$#v7$#https://twitter.com/rhenium_nv$#t7$#string$#" 45 | ); 46 | }); 47 | 48 | test("J2E-escape", () => { 49 | expect( 50 | json2emap({ 51 | id: 123, 52 | "\\name": "rhenium$#", 53 | accounts: [ 54 | { 55 | type: "github", 56 | link: "https://github.com/rheniumNV", 57 | }, 58 | { 59 | type: "twitter", 60 | link: "https://twitter.com/rhenium_nv", 61 | }, 62 | ], 63 | }) 64 | ).toEqual( 65 | "l$#7$#v$#k0$#id$#v0$#123$#t0$#number$#k1$#\\\\name$#v1$#rhenium$\\#$#t1$#string$#k2$#accounts.length$#v2$#2$#t2$#number$#k3$#accounts_0_.type$#v3$#github$#t3$#string$#k4$#accounts_0_.link$#v4$#https://github.com/rheniumNV$#t4$#string$#k5$#accounts_1_.type$#v5$#twitter$#t5$#string$#k6$#accounts_1_.link$#v6$#https://twitter.com/rhenium_nv$#t6$#string$#" 66 | ); 67 | }); 68 | 69 | test("J2E-escape-multi", () => { 70 | expect( 71 | json2emap({ 72 | id: 123, 73 | "\\name": "rhenium$#", 74 | accounts: [ 75 | { 76 | "typ\\e\\": "github", 77 | link: "https://github.com/rheniumNV", 78 | }, 79 | { 80 | type: "twitter", 81 | link: "https://twitter.com/rhenium_nv", 82 | }, 83 | ], 84 | }) 85 | ).toEqual( 86 | "l$#7$#v$#k0$#id$#v0$#123$#t0$#number$#k1$#\\\\name$#v1$#rhenium$\\#$#t1$#string$#k2$#accounts.length$#v2$#2$#t2$#number$#k3$#accounts_0_.typ\\\\e\\\\$#v3$#github$#t3$#string$#k4$#accounts_0_.link$#v4$#https://github.com/rheniumNV$#t4$#string$#k5$#accounts_1_.type$#v5$#twitter$#t5$#string$#k6$#accounts_1_.link$#v6$#https://twitter.com/rhenium_nv$#t6$#string$#" 87 | ); 88 | }); 89 | 90 | test("J2E-return-whitespace", () => { 91 | expect(json2emap({ id: "\n \n" })).toEqual( 92 | "l$#1$#v$#k0$#id$#v0$#\n \n$#t0$#string$#" 93 | ); 94 | }); 95 | 96 | const json1 = require("./test1.json"); 97 | const json1res = fs.readFileSync("./test/test1.result").toString(); 98 | 99 | test("J2E-2", () => { 100 | expect(json2emap(json1)).toEqual(json1res); 101 | }); 102 | --------------------------------------------------------------------------------