Comments (6)
Did you read any of the docs? Which ones?
I need to know which of these cases it is, as each one will require a different response from me
- Docs not clear (means I have to make changes to the docs)
- Docs not found (means I have to figure out why you couldn't find it after searching)
- Docs not read (means I have to do nothing except point you to the docs).
from go.
Thanks for taking the time to answer.
So I had read the README and the online doc at http://godoc.org/github.com/ugorji/go/codec (the first part and enough of the API doc to get some encoding/decoding code working), did a web search for something like "golang messagepack string" but didn't find anything relevant.
This morning I came across this RawToString flag in the MsgpackHandle type, I tried it out and it seems to give the expected behavior, but I'm not sure I understand why, the comment states "RawToString controls how raw bytes are decoded into a nil interface{}".
By "raw bytes" do you mean that the encoder has produced a Binary family element (BIN8/16/32) when it encoded the string? If so then that's not what I was intuitively expecting, and I didn't see any comments about this behavior in the doc I've read.
from go.
Ah. K.
The messagepack spec was updated in 2013 to include a separate string vs binary type. However, most people are not aware of this spec update, and different encoders treat the raw type as either string or binary.
By default, we maintain compatibility with the old spec and encode both []byte and string as RAW, and decode RAW as []byte.
go-codec then gives users the option (via MsgpackHandle) to configure what to decode RAW as when decoding into a nil interface (ie interface{} with no type).
In your case, the option is MsgpackHandle.RawToString as you found.
The docs for these would have been at:
- API documentation for MsgpackHandle (as you found)
- Primer (a brand new article I wrote, which may help): http://ugorji.net/blog/go-codec-primer#messagepack
For where the term 'raw' originated, it is in the msgpack spec:
https://github.com/msgpack/msgpack/blob/master/spec.md#user-content-type-system
P.S. old spec is at https://github.com/msgpack/msgpack/blob/master/spec-old.md
from go.
P.S. To be fully compatible with the new spec, you should set the following options:
WriteExt=true
This way, we encode []byte using Bin(8|16|32) and string using Str(8|16|32), and decode as same.
Without this flag, we are compatible with the legacy spec, and encode both []byte and string as Str(8|16|32), and use the value of RawToString to decide how to decode Str(8|16|32).
Note: In legacy spec, only Raw existed. In new spec, Raw is renamed to Str, and a new Bin was introduced. The main goal of the updated spec was to resolve the ambiguity.
Hope this helps.
I will update the Primer section on MessagePack to make this clear, and also may introduce a helper function for creating a MessagePack handle which is configured for this like NewMsgpackHandle(legacyMode bool). I don't like this because it limits specs, so I may leave it as it is and just update the primer. Let me think some more, and I am open to suggestions.
from go.
FYI: I have updated http://ugorji.net/blog/go-codec-primer#messagepack to clarify this.
from go.
Thanks for the update :)
from go.
Related Issues (20)
- omitempty handling for arrays disagrees with the standard encoding/json HOT 2
- codec.MsgpackHandle.RawToString is confusingly named HOT 2
- Go 1.20 - invalid memory address or nil pointer dereference HOT 5
- Msgpack deserialization with codecgen produces wrong result HOT 3
- Unexpected encoding with interface{} key using Canonical and MapKeyAsString.
- Decoding a slice of structs encoded as arrays into a slice of structs without the same number of fields HOT 2
- Duplicate key inside of a map triggers a "cannot decode unsigned integer" msgpack decode error HOT 1
- Efficient direct conversion between supported formats? HOT 2
- Expose Handle.Name() in some form through Encoder and Decoder for use by codec.Selfer HOT 1
- `io.ErrUnexpectedEOF` should be returned instead of `io.EOF` when only partial data is present
- Add `DecodeOptions.IntType` and `DecodeOptions.FloatType` ? HOT 1
- Docs do not seem to mention case-sensitive field handling
- mem leak? HOT 1
- Tests use rand.Intn(1) for boolean rand, but always returns 0
- Decoder accepts indefinite-length CBOR strings containing chunks with mismatched major types HOT 1
- UTF-8 validation isn't applied to CBOR text strings nested inside an indefinite-length text string HOT 2
- Add support for github.com/go-json-experiment/json / UnmarshalerV2 ? HOT 1
- runtime error with golang master (furture go 1.22)
- Leading zeros is not allowed in rfc7159
- attr.go
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go.