question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem with serializing/deserializing index to/from JSON

See original GitHub issue

After 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:closed
  • Created 10 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
olivernncommented, Oct 20, 2013

As @brockfanning mentioned you must serialise the index by using JSON.stringify:

var idx = lunr(function(){
      this.ref('id')
      this.field('title', { boost: 10 })
      this.field('text')
})

idx.add(doc)

JSON.stringify(idx)

The reason JSON.stringify is important is that it will recursively call toJSON on every object that toJSON 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, specifically lunr.Store. The return value from just calling toJSON on lunr.Store and the output of JSON.parse(JSON.stringify(store)) are different. This is because JSON.stringify is going through each item in the store and making sure toJSON is called, just calling lunr.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.

0reactions
brockfanningcommented, Oct 10, 2013

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:

index = lunr(function(){
      this.ref('id');
      this.field('title', {boost: 10});
      this.field('text');
});
index.add({id:1,title:'My Title',text:'My Text'});
indexStringified = JSON.stringify(index.toJSON());
indexParsed = JSON.parse(indexStringified);
newIndex = lunr.Index.load(indexParsed);
newIndex.search('text');
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found