Source File
flags.go
Belonging Package
encoding/json/internal/jsonflags
// Copyright 2023 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.//go:build goexperiment.jsonv2// jsonflags implements all the optional boolean flags.// These flags are shared across both "json", "jsontext", and "jsonopts".package jsonflagsimport// Bools represents zero or more boolean flags, all set to true or false.// The least-significant bit is the boolean value of all flags in the set.// The remaining bits identify which particular flags.//// In common usage, this is OR'd with 0 or 1. For example:// - (AllowInvalidUTF8 | 0) means "AllowInvalidUTF8 is false"// - (Multiline | Indent | 1) means "Multiline and Indent are true"type Bools uint64func (Bools) (internal.NotForPublicUse) {}const (// AllFlags is the set of all flags.AllFlags = AllCoderFlags | AllArshalV2Flags | AllArshalV1Flags// AllCoderFlags is the set of all encoder/decoder flags.AllCoderFlags = (maxCoderFlag - 1) - initFlag// AllArshalV2Flags is the set of all v2 marshal/unmarshal flags.AllArshalV2Flags = (maxArshalV2Flag - 1) - (maxCoderFlag - 1)// AllArshalV1Flags is the set of all v1 marshal/unmarshal flags.AllArshalV1Flags = (maxArshalV1Flag - 1) - (maxArshalV2Flag - 1)// NonBooleanFlags is the set of non-boolean flags,// where the value is some other concrete Go type.// The value of the flag is stored within jsonopts.Struct.NonBooleanFlags = 0 |Indent |IndentPrefix |ByteLimit |DepthLimit |Marshalers |Unmarshalers// DefaultV1Flags is the set of booleans flags that default to true under// v1 semantics. None of the non-boolean flags differ between v1 and v2.DefaultV1Flags = 0 |AllowDuplicateNames |AllowInvalidUTF8 |EscapeForHTML |EscapeForJS |PreserveRawStrings |Deterministic |FormatNilMapAsNull |FormatNilSliceAsNull |MatchCaseInsensitiveNames |CallMethodsWithLegacySemantics |FormatByteArrayAsArray |FormatBytesWithLegacySemantics |FormatDurationAsNano |MatchCaseSensitiveDelimiter |MergeWithLegacySemantics |OmitEmptyWithLegacySemantics |ParseBytesWithLooseRFC4648 |ParseTimeWithLooseRFC3339 |ReportErrorsWithLegacySemantics |StringifyWithLegacySemantics |UnmarshalArrayFromAnyLength// AnyWhitespace reports whether the encoded output might have any whitespace.AnyWhitespace = Multiline | SpaceAfterColon | SpaceAfterComma// WhitespaceFlags is the set of flags related to whitespace formatting.// In contrast to AnyWhitespace, this includes Indent and IndentPrefix// as those settings take no effect if Multiline is false.WhitespaceFlags = AnyWhitespace | Indent | IndentPrefix// AnyEscape is the set of flags related to escaping in a JSON string.AnyEscape = EscapeForHTML | EscapeForJS// CanonicalizeNumbers is the set of flags related to raw number canonicalization.CanonicalizeNumbers = CanonicalizeRawInts | CanonicalizeRawFloats)// Encoder and decoder flags.const (initFlag Bools = 1 << iota // reserved for the boolean value itselfAllowDuplicateNames // encode or decodeAllowInvalidUTF8 // encode or decodeWithinArshalCall // encode or decode; for internal use by json.Marshal and json.UnmarshalOmitTopLevelNewline // encode only; for internal use by json.Marshal and json.MarshalWritePreserveRawStrings // encode onlyCanonicalizeRawInts // encode onlyCanonicalizeRawFloats // encode onlyReorderRawObjects // encode onlyEscapeForHTML // encode onlyEscapeForJS // encode onlyMultiline // encode onlySpaceAfterColon // encode onlySpaceAfterComma // encode onlyIndent // encode only; non-boolean flagIndentPrefix // encode only; non-boolean flagByteLimit // encode or decode; non-boolean flagDepthLimit // encode or decode; non-boolean flagmaxCoderFlag)// Marshal and Unmarshal flags (for v2).const (_ Bools = (maxCoderFlag >> 1) << iotaStringifyNumbers // marshal or unmarshalDeterministic // marshal onlyFormatNilMapAsNull // marshal onlyFormatNilSliceAsNull // marshal onlyOmitZeroStructFields // marshal onlyMatchCaseInsensitiveNames // marshal or unmarshalDiscardUnknownMembers // marshal onlyRejectUnknownMembers // unmarshal onlyMarshalers // marshal only; non-boolean flagUnmarshalers // unmarshal only; non-boolean flagmaxArshalV2Flag)// Marshal and Unmarshal flags (for v1).const (_ Bools = (maxArshalV2Flag >> 1) << iotaCallMethodsWithLegacySemantics // marshal or unmarshalFormatByteArrayAsArray // marshal or unmarshalFormatBytesWithLegacySemantics // marshal or unmarshalFormatDurationAsNano // marshal or unmarshalMatchCaseSensitiveDelimiter // marshal or unmarshalMergeWithLegacySemantics // unmarshalOmitEmptyWithLegacySemantics // marshalParseBytesWithLooseRFC4648 // unmarshalParseTimeWithLooseRFC3339 // unmarshalReportErrorsWithLegacySemantics // marshal or unmarshalStringifyWithLegacySemantics // marshal or unmarshalStringifyBoolsAndStrings // marshal or unmarshal; for internal use by jsonv2.makeStructArshalerUnmarshalAnyWithRawNumber // unmarshal; for internal use by jsonv1.Decoder.UseNumberUnmarshalArrayFromAnyLength // unmarshalmaxArshalV1Flag)// bitsUsed is the number of bits used in the 64-bit boolean flagsconst bitsUsed = 42// Static compile check that bitsUsed and maxArshalV1Flag are in sync.const _ = uint64((1<<bitsUsed)-maxArshalV1Flag) + uint64(maxArshalV1Flag-(1<<bitsUsed))// Flags is a set of boolean flags.// If the presence bit is zero, then the value bit must also be zero.// The least-significant bit of both fields is always zero.//// Unlike Bools, which can represent a set of bools that are all true or false,// Flags represents a set of bools, each individually may be true or false.type Flags struct{ Presence, Values uint64 }// Join joins two sets of flags such that the latter takes precedence.func ( *Flags) ( Flags) {// Copy over all source presence bits over to the destination (using OR),// then invert the source presence bits to clear out source value (using AND-NOT),// then copy over source value bits over to the destination (using OR).// e.g., dst := Flags{Presence: 0b_1100_0011, Value: 0b_1000_0011}// e.g., src := Flags{Presence: 0b_0101_1010, Value: 0b_1001_0010}.Presence |= .Presence // e.g., 0b_1100_0011 | 0b_0101_1010 -> 0b_110_11011.Values &= ^.Presence // e.g., 0b_1000_0011 & 0b_1010_0101 -> 0b_100_00001.Values |= .Values // e.g., 0b_1000_0001 | 0b_1001_0010 -> 0b_100_10011}// Set sets both the presence and value for the provided bool (or set of bools).func ( *Flags) ( Bools) {// Select out the bits for the flag identifiers (everything except LSB),// then set the presence for all the identifier bits (using OR),// then invert the identifier bits to clear out the values (using AND-NOT),// then copy over all the identifier bits to the value if LSB is 1.// e.g., fs := Flags{Presence: 0b_0101_0010, Value: 0b_0001_0010}// e.g., f := 0b_1001_0001:= uint64() &^ uint64(1) // e.g., 0b_1001_0001 & 0b_1111_1110 -> 0b_1001_0000.Presence |= // e.g., 0b_0101_0010 | 0b_1001_0000 -> 0b_1101_0011.Values &= ^ // e.g., 0b_0001_0010 & 0b_0110_1111 -> 0b_0000_0010.Values |= uint64(&1) * // e.g., 0b_0000_0010 | 0b_1001_0000 -> 0b_1001_0010}// Get reports whether the bool (or any of the bools) is true.// This is generally only used with a singular bool.// The value bit of f (i.e., the LSB) is ignored.func ( Flags) ( Bools) bool {return .Values&uint64() > 0}// Has reports whether the bool (or any of the bools) is set.// The value bit of f (i.e., the LSB) is ignored.func ( Flags) ( Bools) bool {return .Presence&uint64() > 0}// Clear clears both the presence and value for the provided bool or bools.// The value bit of f (i.e., the LSB) is ignored.func ( *Flags) ( Bools) {// Invert f to produce a mask to clear all bits in f (using AND).// e.g., fs := Flags{Presence: 0b_0101_0010, Value: 0b_0001_0010}// e.g., f := 0b_0001_1000:= uint64(^) // e.g., 0b_0001_1000 -> 0b_1110_0111.Presence &= // e.g., 0b_0101_0010 & 0b_1110_0111 -> 0b_0100_0010.Values &= // e.g., 0b_0001_0010 & 0b_1110_0111 -> 0b_0000_0010}
![]() |
The pages are generated with Golds v0.7.9-preview. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |