javascript - How to encode a text diagram into json? -


i'm trying encode oauth2 diagram string in json object. when "decode" using builtin js parsejson function output looks broken. idea why or how should encode ?

plunkr

function show(){   var diagram = '  +----------+\      | resource |\      |  owner   |\      |          |\      +----------+\           ^\           |\          (b)\      +----|-----+          client identifier     +---------------+\      |         -+----(a)-- & redirection uri --->|               |\      |  user-   |                                | authorization |\      |  agent  -|----(b)-- user authenticates -->|     server    |\      |          |                                |               |\      |          |<---(c)--- redirection uri ----<|               |\      |          |          access token     +---------------+\      |          |            in fragment\      |          |                                +---------------+\      |          |----(d)--- redirection uri ---->|   web-hosted  |\      |          |          without fragment      |     client    |\      |          |                                |    resource   |\      |     (f)  |<---(e)------- script ---------<|               |\      |          |                                +---------------+\      +-|--------+\        |    |\       (a)  (g) access token\        |    |\        ^    v\      +---------+\      |         |\      |  client |\      |         |\      +---------+\ '   x =  json.stringify(diagram)   document.write(json.parse(x)); } 

result

+----------+ | resource | | owner | | | +----------+ ^ | (b) +----|-----+ client identifier +---------------+ | -+----(a)-- & redirection uri --->| | | user- | | authorization | | agent -|----(b)-- user authenticates -->| server | | | | | | |<---(c)--- redirection uri ----<| | | | access token +---------------+ | | in fragment | | +---------------+ | |----(d)--- redirection uri ---->| web-hosted | | | without fragment | client | | | | resource | | (f) |<---(e)------- script ---------<| | | | +---------------+ +-|--------+ | | (a) (g) access token | | ^ v +---------+ | | | client | | | +---------+ 

update: doesn't work either

// code goes here  function show(){   var diagram = '  +----------+\n\      | resource |\n\      |  owner   |\n\      |          |\n\      +----------+\n\           ^\n\           |\n\          (b)\n\      +----|-----+          client identifier     +---------------+\n\      |         -+----(a)-- & redirection uri --->|               |\n\      |  user-   |                                | authorization |\n\      |  agent  -|----(b)-- user authenticates -->|     server    |\n\      |          |                                |               |\n\      |          |<---(c)--- redirection uri ----<|               |\n\      |          |          access token     +---------------+\n\      |          |            in fragment\n\      |          |                                +---------------+\n\      |          |----(d)--- redirection uri ---->|   web-hosted  |\n\      |          |          without fragment      |     client    |\n\      |          |                                |    resource   |\n\      |     (f)  |<---(e)------- script ---------<|               |\n\      |          |                                +---------------+\n\      +-|--------+\n\        |    |\n\       (a)  (g) access token\n\        |    |\n\        ^    v\n\      +---------+\n\      |         |\n\      |  client |\n\      |         |\n\      +---------+\n\ '   x =  json.stringify(diagram)   document.write("<p>"+ json.parse(x) +"</p>"); } 

the line breaks in source code listing not part of string itself. need add line break characters (\n) if want them part of string.

if convert string json , convert json string, may or may not encounter other issues. above first issue you'll need address.

one trick i've seen around multiline weirdness not have multiline string @ instead have array of strings each string represents 1 line in multiline string.


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -