-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
std/encoding, std/encoding/json: improve custom encoding and decoding…
… method support
- Loading branch information
1 parent
5f8f311
commit cb2eae9
Showing
3 changed files
with
105 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2024 The Jule Programming Language. | ||
// Use of this source code is governed by a BSD 3-Clause | ||
// license that can be found in the LICENSE file. | ||
|
||
// Implements custom encoder method for JSON encoding. | ||
// For types with this method, this method is called instead of the default | ||
// encoding strategy and custom encoding is performed. The returned bytes must | ||
// be a valid JSON value. Otherwise, EncodeError.EncodeJSON exception is thrown. | ||
// Throwing any exception is considered valid. The thrown exception will be | ||
// forwarded by the Encode. Successful encoding should not throw any exceptions. | ||
trait JSONEncoder { | ||
fn EncodeJSON(self)!: []byte | ||
} | ||
|
||
// Implements custom decoder method for JSON decoding. | ||
// For types with this method, this method is called instead of the default | ||
// decoding strategy and custom decoding is performed. The data parameter is the | ||
// corresponding data equivalent and is always a validated, error-free JSON data. | ||
// It is a mutable copy taken from the data used for decoding, so any change may | ||
// cause mutation in the main data. According to the defined behavior, | ||
// decoder methods should not mutate the content of the data. | ||
// Throwing any exception is considered valid. The thrown exception will be | ||
// forwarded by the Decode. Successful decoding should not throw any exceptions | ||
// and self should be changed as required. | ||
trait JSONDecoder { | ||
fn DecodeJSON(mut self, data: []byte)! | ||
} | ||
|
||
// Implements custom encoder method for text encoding. | ||
// For types with this method, this method is called instead of the default | ||
// encoding strategy and custom encoding is performed. Throwing any exception | ||
// is considered valid. The thrown exception will be forwarded by the encoder. | ||
// Successful encoding should not throw any exceptions. | ||
// It should return UTF-8 encoded text in bytes. | ||
trait TextEncoder { | ||
fn EncodeText(self)!: []byte | ||
} | ||
|
||
// Implements custom decoder method for text decoding. | ||
// For types with this method, this method is called instead of the default | ||
// decoding strategy and custom decoding is performed. The data parameter is the | ||
// corresponding data equivalent and is always UTF-8 encoded text in bytes. | ||
// It may be a mutable copy taken from the data used for decoding, so any change may | ||
// cause mutation in the main data. According to the defined behavior, | ||
// decoder methods should not mutate the content of the data. | ||
// Throwing any exception is considered valid. The thrown exception will be | ||
// forwarded by the decoder. Successful decoding should not throw any exceptions | ||
// and self should be changed as required. | ||
trait TextDecoder { | ||
fn DecodeText(mut self, data: []byte)! | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters