+// Parses a string serialized by serializeJsonStringIfNeeded.
+static std::string deSerializeJsonStringIfNeeded(std::istream &is)
+{
+ std::ostringstream tmp_os;
+ bool expect_initial_quote = true;
+ bool is_json = false;
+ bool was_backslash = false;
+ for(;;)
+ {
+ char c = is.get();
+ if(is.eof())
+ break;
+ if(expect_initial_quote && c == '"')
+ {
+ tmp_os << c;
+ is_json = true;
+ }
+ else if(is_json)
+ {
+ tmp_os << c;
+ if(was_backslash)
+ was_backslash = false;
+ else if(c == '\\')
+ was_backslash = true;
+ else if(c == '"')
+ break; // Found end of string
+ }
+ else
+ {
+ if(c == ' ')
+ {
+ // Found end of word
+ is.unget();
+ break;
+ }
+ else
+ {
+ tmp_os << c;
+ }
+ }
+ expect_initial_quote = false;
+ }
+ if(is_json)
+ {
+ std::istringstream tmp_is(tmp_os.str(), std::ios::binary);
+ return deSerializeJsonString(tmp_is);
+ }
+ else
+ return tmp_os.str();
+}
+
+
+ItemStack::ItemStack(std::string name_, u16 count_,
+ u16 wear_, std::string metadata_,
+ IItemDefManager *itemdef)
+{
+ name = itemdef->getAlias(name_);
+ count = count_;
+ wear = wear_;
+ metadata = metadata_;
+
+ if(name.empty() || count == 0)
+ clear();
+ else if(itemdef->get(name).type == ITEM_TOOL)
+ count = 1;
+}
+
+void ItemStack::serialize(std::ostream &os) const