glenjamin / transit-immutable-js

Transit serialisation for Immutable.js
MIT License
250 stars 31 forks source link

Loading JSON Unexpected Token "~" #17

Closed margaretjoanmiller closed 8 years ago

margaretjoanmiller commented 8 years ago

I'm trying to send my redux state preloaded for a universal react app (with the format:

const initialState = Immutable.fromJS({
      postReducer: {
        posts: [],
        post: {},
      },
      routerReducer: {
        location: {},
      },
});

) and I am sending this with ...window.__INITIAL_STATE__ = ${transit.toJSON(initialState)};... (which is, as denoted by the ``, part of a template string that is sent on every page request) to be reevaluated withconst store = configureStore(transit.fromJSON(window.__INITIAL_STATE__));. However, this produces the errorUncaught SyntaxError: Unexpected token ~ in JSON at position 0` and the app, from what I can tell, does not actually load; it just displays what was prerendered. The JSON that is sent, according to chrome dev tools, is:

window.__INITIAL_STATE__ = ["~#iM",["postReducer",["^0",["posts",["~#iL",[["^0",["id","cinjpzjn40000znrfatnm2yb9","name","asd","title","zdg","content","sasdg","slug","zdg","dateadded","2016-04-28T03:17:33.615Z","updateddate",null]],["^0",["id","cinjphi3z000043rfsd81hcf2","name","ad","title","sdg","content","sg","slug","sdg","dateadded","2016-04-28T03:03:31.823Z","updateddate",null]],["^0",["id","cin86bt1o0000fyrfx9fwx3c5","name","adsf","title","sddasf`","content","werewr","slug","sddasf","dateadded","2016-04-20T01:21:45.420Z","updateddate",null]],["^0",["id","cin6vwiu000005mrfpnu2ebwk","name","asf","title","sewrwer324`45454","content","vcvbrt254545","slug","sewrwer32445454","dateadded","2016-04-19T03:42:10.008Z","updateddate",null]],["^0",["id","cin6s1dvp0000ngrfrfg0k765","name","asdf","title","sadg","content","sadg","slug","sadg","dateadded","2016-04-19T01:53:58.404Z","updateddate",null]],["^0",["id","cin6rs8ah0000bhrfjqnlrdmd","name","252535dsfs","title","as","content","saxcvbxcvb","slug","as","dateadded","2016-04-19T01:46:51.257Z","updateddate",null]],["^0",["id","cin6riqrr00001vrfmtvjwe2y","name","sdf","title","sdg","content","sadg","slug","sdg","dateadded","2016-04-19T01:39:28.647Z","updateddate",null]],["^0",["id","cin6r9h3a0001qjrfujhcwi5r","name","asdf","title","sdg","content","asdg","slug","sdg","dateadded","2016-04-19T01:32:16.198Z","updateddate",null]],["^0",["id","cin6r46f10000qjrf9hkw3ihd","name","sd","title","sdg","content","sggsdg","slug","sdg","dateadded","2016-04-19T01:28:09.085Z","updateddate",null]],["^0",["id","cin49d3uu00002srf1w62ca6p","name","asdg","title","sdfg","content","sdhsdfh","slug","sdfg","dateadded","2016-04-17T07:35:40.230Z","updateddate",null]],["^0",["id","cin44q2yz0000f6rfjjdsjxmn","name","asdg`","title","4346gfd","content","drdhsh","slug","4346gfd","dateadded","2016-04-17T05:25:47.531Z","updateddate",null]],["^0",["id","cin2n5fl800005lrf04vdpcz1","name","sdfh`","title","fdf6dfg","content","fgsdyfbhfhsdfh","slug","fdf6dfg","dateadded","2016-04-16T04:26:04.460Z","updateddate",null]],["^0",["id","cin2lnjm70000uwrf93ees739","name","asdf","title","dsgasdg","content","sg","slug","dsgasdg","dateadded","2016-04-16T03:44:10.255Z","updateddate",null]],["^0",["id","cin2l5t880000ohrf6mh7ubbh","name","464erxfxf75","title","57dgfhftucd","content","676fghkghk","slug","57dgfhftucd","dateadded","2016-04-16T03:30:22.904Z","updateddate",null]],["^0",["id","cin19lw6r0000dnrft3gvibpc","name","sdfg`","title","jdffgjfgj","content","dshdfh","slug","jdffgjfgj","dateadded","2016-04-15T05:19:11.667Z","updateddate",null]],["^0",["id","cin19hwn50000m4rfeahdo0ox","name","dfg","title","f57567","content","696kfghk","slug","f57567","dateadded","2016-04-15T05:16:05.633Z","updateddate",null]],["^0",["id","cin17hypw0000h6rfabambdyw","name","fghf","title","fdd","content","45464","slug","fdd","dateadded","2016-04-15T04:20:09.092Z","updateddate",null]],["^0",["id","cin0w9gwb0001vyrftw7o48c3","name","32456","title","34246","content","346346346","slug","34246","dateadded","2016-04-14T23:05:36.971Z","updateddate",null]],["^0",["id","cimyd1chz00021lrfqhateszf","name","dfsgsdfg","title","zxcvcxzv","content","565hgfj56u","slug","zxcvcxzv","dateadded","2016-04-13T04:31:52.967Z","updateddate",null]],["^0",["id","cimyd161100011lrfolwamdbf","name","gsdfg","title","567","content","dfjgfjg\n","slug","567","dateadded","2016-04-13T04:31:44.581Z","updateddate",null]],["^0",["id","cinjpzjn40000znrfatnm2yb9","name","asd","title","zdg","content","sasdg","slug","zdg","dateadded","2016-04-28T03:17:33.615Z","updateddate",null]],["^0",["id","cinjphi3z000043rfsd81hcf2","name","ad","title","sdg","content","sg","slug","sdg","dateadded","2016-04-28T03:03:31.823Z","updateddate",null]],["^0",["id","cin86bt1o0000fyrfx9fwx3c5","name","adsf","title","sddasf`","content","werewr","slug","sddasf","dateadded","2016-04-20T01:21:45.420Z","updateddate",null]],["^0",["id","cin6vwiu000005mrfpnu2ebwk","name","asf","title","sewrwer324`45454","content","vcvbrt254545","slug","sewrwer32445454","dateadded","2016-04-19T03:42:10.008Z","updateddate",null]],["^0",["id","cin6s1dvp0000ngrfrfg0k765","name","asdf","title","sadg","content","sadg","slug","sadg","dateadded","2016-04-19T01:53:58.404Z","updateddate",null]],["^0",["id","cin6rs8ah0000bhrfjqnlrdmd","name","252535dsfs","title","as","content","saxcvbxcvb","slug","as","dateadded","2016-04-19T01:46:51.257Z","updateddate",null]],["^0",["id","cin6riqrr00001vrfmtvjwe2y","name","sdf","title","sdg","content","sadg","slug","sdg","dateadded","2016-04-19T01:39:28.647Z","updateddate",null]],["^0",["id","cin6r9h3a0001qjrfujhcwi5r","name","asdf","title","sdg","content","asdg","slug","sdg","dateadded","2016-04-19T01:32:16.198Z","updateddate",null]],["^0",["id","cin6r46f10000qjrf9hkw3ihd","name","sd","title","sdg","content","sggsdg","slug","sdg","dateadded","2016-04-19T01:28:09.085Z","updateddate",null]],["^0",["id","cin49d3uu00002srf1w62ca6p","name","asdg","title","sdfg","content","sdhsdfh","slug","sdfg","dateadded","2016-04-17T07:35:40.230Z","updateddate",null]],["^0",["id","cin44q2yz0000f6rfjjdsjxmn","name","asdg`","title","4346gfd","content","drdhsh","slug","4346gfd","dateadded","2016-04-17T05:25:47.531Z","updateddate",null]],["^0",["id","cin2n5fl800005lrf04vdpcz1","name","sdfh`","title","fdf6dfg","content","fgsdyfbhfhsdfh","slug","fdf6dfg","dateadded","2016-04-16T04:26:04.460Z","updateddate",null]],["^0",["id","cin2lnjm70000uwrf93ees739","name","asdf","title","dsgasdg","content","sg","slug","dsgasdg","dateadded","2016-04-16T03:44:10.255Z","updateddate",null]],["^0",["id","cin2l5t880000ohrf6mh7ubbh","name","464erxfxf75","title","57dgfhftucd","content","676fghkghk","slug","57dgfhftucd","dateadded","2016-04-16T03:30:22.904Z","updateddate",null]],["^0",["id","cin19lw6r0000dnrft3gvibpc","name","sdfg`","title","jdffgjfgj","content","dshdfh","slug","jdffgjfgj","dateadded","2016-04-15T05:19:11.667Z","updateddate",null]],["^0",["id","cin19hwn50000m4rfeahdo0ox","name","dfg","title","f57567","content","696kfghk","slug","f57567","dateadded","2016-04-15T05:16:05.633Z","updateddate",null]],["^0",["id","cin17hypw0000h6rfabambdyw","name","fghf","title","fdd","content","45464","slug","fdd","dateadded","2016-04-15T04:20:09.092Z","updateddate",null]],["^0",["id","cin0w9gwb0001vyrftw7o48c3","name","32456","title","34246","content","346346346","slug","34246","dateadded","2016-04-14T23:05:36.971Z","updateddate",null]],["^0",["id","cimyd1chz00021lrfqhateszf","name","dfsgsdfg","title","zxcvcxzv","content","565hgfj56u","slug","zxcvcxzv","dateadded","2016-04-13T04:31:52.967Z","updateddate",null]],["^0",["id","cimyd161100011lrfolwamdbf","name","gsdfg","title","567","content","dfjgfjg\n","slug","567","dateadded","2016-04-13T04:31:44.581Z","updateddate",null]]]],"post",["^0",[]]]],"routerReducer",["^0",["location",["^0",[]]]]]];
glenjamin commented 8 years ago

The quirk is that fromJSON requires a string, not the object.

There's an open issue with the main transit module asking about accepting an object instead.

glenjamin commented 8 years ago

See https://github.com/cognitect/transit-js/issues/23 for the upstream issue which would enable passing the array / object directly.