Character encoding of a `Data` from a `URLSession` to create objects from JSON in Swift

In Swift, I am trying to downloaded JSON data from a URL, then use it to create & populate a Swift object.

I do not know the character encoding of the returned JSON.

I have seen some uses of JSONDecoder#decode<T>(_ type: T.Type, from data: Data).

How does the above function know what character encoding to use?

The Data that I’ve seen used is from an argument to the completionHandler argument from the following function from URLSession:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>func dataTask(
with request: URLRequest,
completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void
) -> URLSessionDataTask
</code>
<code>func dataTask( with request: URLRequest, completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void ) -> URLSessionDataTask </code>
func dataTask(
    with request: URLRequest,
    completionHandler: @escaping @Sendable (Data?, URLResponse?, (any Error)?) -> Void
) -> URLSessionDataTask

I don’t see any character encoding info in the Data class, but I do see it in URLResponse#textEncodingName.

Does Data somehow know what its character encoding is? Or does JSONDecoder#decode<T>(…) try to autodetect the character encoding? Or?

If Data doesn’t contain the character encoding info itself, how can I use it from URLResponse#textEncodingName in some function to create & populate a new object?

Also, if the input is is not proper JSON, I want to output the non-JSON text. From what I’ve seen, I’m supposed to use String(data:encoding:) to convert from Data to String?. I currently do not have the URLResponse available to me where I need to call this (I just have the Data) due to existing code from someone else.

If the Data has the character encoding, is there a function I can call that just takes the Data without requiring the String.Encoding argument? Or is there some way to read the character encoding from the Data so I can use it to provide the String.Encoding argument?

If I can avoid directly using the URLResponse, it will save me from needing to refactor everything to make the URLResponse available in certain places.

0

Data does not know what encoding it is encoded with. Data is just a collection of bytes. It doesn’t even necessarily represent text at all.

Though JSONDecoder does not document what encodings it supports, JSONSerialization.jsonObject(with:options:) does.

The data must be in one of the 5 supported encodings listed in the JSON specification: UTF-8, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE. The data may or may not have a BOM.

As far as I know, JSONDeocder uses the JSONSerialization APIs under the hood, in which case it would support the same encodings too. From a quick check, this is at least true for the JSONDecoder implementation in swift-foundation. Here is how swift-foundation autodetect the encoding.

So if you can assume that the encoding will be one of those listed above, you can just use JSONDecoder as usual.

Otherwise, you would have to read the textEncodingName from the URLResponse (or autodetect it), decode the text, and re-encode it into data using a supported encoding.

You also mentioned that you want to get the response data as plain text if it is not valid JSON. In that case, you must get the encoding via textEncodingName (or autodetect it), as well.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật