id | title |
---|---|
projected-names |
Projected names |
:::warning Projected names is a legacy feature and will be phased out. Please use Encoded Names instead for changing the name over the wire. :::
There is some cases where the name you have in TypeSpec might differ from the name over the wire or for a certain language.
List of known targets.
- Wire:
json
: Configure JSON representation of dataxml
: Configure XML representation of data
- Language:
csharp
: Configure C# generated codejava
: Configure Java generated codepython
: Configure python generated codejavascript
: Configure javascript generated codeswift
: Configure swift generated codec
: Configure C generated code
- Type:
client
: Configure output for the clientserver
: Configure output for the server
To update the name of a TypeSpec entity you can use the @projectedName
decorator. This decorator takes 2 parameters:
string
target name. See known targetsstring
projected name. Whatever the name should be in the given target.
Example:
model Foo {
// Specify that when serializing to JSON `expireAt` property should be named `exp`
@projectedName("json", "exp")
expireAt: string;
}
The decorator is just a syntax sugar for the target
projection behind the scenes. In more complex cases you might want to just implement the projection manually.
model Foo {
expireAt: string;
}
projection Foo#target {
to(targetName) {
if targetName == "json" {
self::rename("exp");
};
}
}
model CertificateAttributes {
@projectedName("json", "nbf")
@projectedName("csharp", "ValidAfter")
notBefore: int32;
@projectedName("json", "exp")
expires: int32;
@projectedName("client", "createdAt")
created: int32;
updated: int32;
}
Json | Typescript | CSharp |
---|---|---|
When serialized to Json property use the json projected name | Typescript didn't provide any projected name so it keep the model as it is. | Model uses the `csharp` projected names and keeps the reference to the JSON name in JsonProperty |
{
"nbf": 1430344421,
"exp": 2208988799,
"created": 1493938289,
"updated": 1493938291
} |
interface Attributes {
notBefore: number;
expires: number;
createdAt: number;
updated: number;
} |
class CertificateAttributes
{
[JsonProperty("nbf")]
public int ValidAfter {get; set;}
[JsonProperty("exp")]
public int Expires {get; set;}
[JsonProperty("created")]
public int CreatedAt {get; set;}
public int Updated {get; set;}
} |