ForeverZer0 / SharpNBT

A pure CLS-compliant C# implementation of the Named Binary Tag (NBT) format specification commonly used with Minecraft applications, allowing easy reading/writing streams and serialization to other formats.
MIT License
25 stars 9 forks source link

How can I revert back to the SNBT string #11

Closed NingLiu1998 closed 10 months ago

NingLiu1998 commented 11 months ago

image

CompoundTag How can to StringNbt ?

NingLiu1998 commented 11 months ago

我的目的是想把 CompoundTag转为下面的格式

My purpose is to convert CompoundTag to the following format

string testNbtStr = "{Count:64b,id:\"minecraft:experience_bottle\"}";
NingLiu1998 commented 11 months ago

Stringify ?but ...

image

image

It might work?

ForeverZer0 commented 11 months ago

I do not completely understand what your problem is or how to help exactly. Is it the capital B instead of a lowercase b causing a problem? Any NBT implementation that follows the spec should accept both formats. There currently isn't an option in the library to specify one over the other, I believe it just uses the capitalized variant by default.

One minor thing to note (unrelated) is that, by convention, this is not a typical use of the byte tag, which is normally reserved for booleans and buffers, not just as integers to represent a count. It will of course still work, but the normal convention for the format is to use a VarInt for field that represents a count. It will result in one extra byte, but the intent will be clearer.

NingLiu1998 commented 11 months ago

@ForeverZer0 我现在想把这个SNB字符串转为一个规范的Json字符串给 网页前端展示使用,然后统一能把Json字符串转回 NBT 字符串,我该怎么做

Now I want to convert this SNB string into a canonical Json string for display on the front end of the webpage, and then I can convert the Json string back into an NBT string. How should I do it?

ForeverZer0 commented 11 months ago

As mentioned in #10, JSON and SNBT are similar, but not the same. If you are serializing to your own type, you will need to implement the logic for encoding it to JSON on that type. Minecraft opting to use stringified-NBT as string values inside JSON tags just makes this more confusing.

There is times when you will receive a string, which is JSON encoded, but within that JSON, it has a string value, which represents NBT, and is not JSON. This makes it so one has to pay strict attention to what the expected format is and how to deccode it properly, as the two formats are not interchangeable.