Problem with serializing/deserializing index to/from JSON
See original GitHub issueAfter making an index like so:
index = lunr(function(){
this.ref('id');
this.field('title', {boost: 10});
this.field('text');
});
And calling index.toJSON() to create a storable representation, and then reloading the index from it:
newIndex = lunr.Index.load(index.toJSON());
The new index seems to be “broken” in some way. Attempting a search, for instance, gives the error TypeError: Cannot read property 'tf' of undefined
Maybe I’m making some simple error here, but it seems a little strange.
Issue Analytics
- State:
- Created 10 years ago
- Comments:10 (1 by maintainers)
Top Results From Across the Web
Cannot find a way to Deserialize Json indexed array using ...
The problem is that it is not a "named" array and it has indexes in place of property name. This array is dynamic,...
Read more >Failed to deserialize JSON when passing local variable record ...
Hi All,. I am new on using JSON serialize and JSON deserialize method. I tried to pass local variable list record from pop...
Read more >After deserialization, while fetching data from JSON if array ...
After deserialization, while fetching data from JSON if array don't have any value the error occur.
Read more >Deserialize JSON Array With Integer Index
So if you use JSONtoApex, you get a class like this: public class jContactsDemo { public class Data { public 1500 1500; public...
Read more >Serialization Error Handling - Json.NET
Json.NET supports error handling during serialization and deserialization. Error handling lets you catch an error and choose whether to handle it and ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
As @brockfanning mentioned you must serialise the index by using
JSON.stringify
:The reason
JSON.stringify
is important is that it will recursively calltoJSON
on every object thattoJSON
returns. In most cases this doesn’t matter (which is why diagnosing this issue is tricky!) however there are a couple of objects that act as stores for other objects, specificallylunr.Store
. The return value from just callingtoJSON
onlunr.Store
and the output ofJSON.parse(JSON.stringify(store))
are different. This is becauseJSON.stringify
is going through each item in the store and making suretoJSON
is called, just callinglunr.Store.prototype.toJSON
does not do this.I think this is a case where the documentation could be improved, making it clearer how to serialise an index as well as marking the
toJSON
methods as private. I am currently working on a big overhaul of the documentation for lunr so hopefully other people will not run into the same problem.Aha, I get the error now too.
Well, not sure why, but apparently stringifying and parsing it is the reason I wasn’t seeing that error in my own app: