Questions tagged [jsonencoder]

Use this tag for `JSONEncoder` questions on Apple platforms.

JSONEncoder is a class, introduced in Swift 4, for encoding types on Apple operating systems that conform to Encodable protocol (or Codable protocol, which is type alias for Encodable & Decodable). This offers a simple mechanism to easily generate JSON from Encodable Swift types. This applies to standard Swift collections, such as Array and Dictionary, but custom types can conform to Encodable to participate in this simplified JSON encoding process.

This replaces/supplements the JSONSerialiation class used in prior Swift versions.

See also:

125 questions
18
votes
2 answers

Elegant way to view JSONEncode() output in swift

var test = [String : String] () test["title"] = "title" test["description"] = "description" let encoder = JSONEncoder() let json = try? encoder.encode(test) How can I see the output of the json? If I use print(json) the only thing I get is…
Amir
  • 523
  • 1
  • 5
  • 24
10
votes
2 answers

Encode a [String: Encodable] dictionary into JSON using JSONEncoder in Swift 4

I am just curious how can I encode a dictionary with String key and Encodable value into JSON. For example: let dict: [String: Encodable] = [ "Int": 1, "Double": 3.14, "Bool": false, "String": "test" ] The keys in this dict are all…
ylorn
  • 241
  • 2
  • 8
7
votes
1 answer

How to preserve key order in a Django JSONField

I am having a hard time preserving the order of keys in a JSON object stored in a Django JSONField. I have tried using a custom encoder and decoder as per the docs, but the JSON object keeps re-ordeing itself: >>>from models import…
trubliphone
  • 4,132
  • 3
  • 42
  • 66
6
votes
1 answer

What is the default date format Swift uses for JSONEncoder?

struct TestEnc: Codable { var date = Date() } let encoder = JSONEncoder() let tenc = TestEnc() let jsonData = try encoder.encode(tenc) let json = String(data: jsonData, encoding: String.Encoding.utf8) print("json:\(json)") This…
GoldenJoe
  • 7,874
  • 7
  • 53
  • 92
6
votes
2 answers

How to encode Dictionary with JSONEncoder in Swift 4

I want to encode Dictionary to json with JSONEncoder. It seems like a Request, receive a dictionary as parameter and encode it to json as http body. The code is looks like this: let dict = ["name": "abcde"] protocol Request { var params:…
鸡肉味嘎嘣脆
  • 233
  • 1
  • 3
  • 15
4
votes
2 answers

Error: Serialization for the format json is not supported

I have integrated symfony messenger bundle and i am trying to encode that message . It was working in Symfony 3.4.4 version . However it is giving above error in Symfony 3.4.28 version. I have traced in symfony serialzer component , it seems that…
4
votes
1 answer

PHP json_encode slow for big arrays

I have some json_encode related issues : i need to use a big array (several 100k items), each with very simple structure (one key, one string value). json_decode works ok, but as soon as i want to json_encode it, it's awfully slow. Since i fully…
MarvinLeRouge
  • 444
  • 5
  • 15
4
votes
1 answer

Swift 4 JSONEncoder can not encode String or Int, But they followed Codable protocol

let encoder = JSONEncoder() do { let encodData = try encoder.encode("test string") // same as Int type print(encodData) // nil } catch let err { print(err.localizedDescription) // The data couldn’t be written because it isn’t in the…
HonQii
  • 43
  • 2
  • 4
3
votes
2 answers

Swift/JSONEncoder: Encoding class containing a nested raw JSON object literal

I have a class in Swift whose structure resembles this: class MyClass { var name: String var data: String } Which could be initialised where data contains a JSON object encoded as a String. var instance = MyClass() instance.name =…
Simon
  • 2,721
  • 1
  • 13
  • 15
3
votes
0 answers

How do I remove escape characters from my encoded URL in a JSON object

I am trying to make a script that sends data in the form of a json document in a POST request. To do this I am using the following code: struct RequestModel: Codable { var api_key: String var service_code: String var description: String …
A Israfil
  • 519
  • 4
  • 25
3
votes
2 answers

Convert html to json in php laravel

Im hitting this url…
Salman Riyaz
  • 808
  • 2
  • 14
  • 37
3
votes
2 answers

JSONEncoder's dateEncodingStrategy not working

I am trying to serialize a struct to a String using Swift 4's Encodable+JSONEncoder. The object can hold heterogenous values like String, Array, Date, Int etc. The used approach works fine with the exception of Date. JSONEncoder's…
justintime
  • 352
  • 3
  • 11
3
votes
2 answers

Store Encodables in a Swift Dictionary

I'm looking to store models objects in a Dictionary and would like to serialize the whole dictionary using JSONEncoder into data and subsequently into a string and save it. The idea is to use Swift 4's out of the box Encodable to ensure anything…
justintime
  • 352
  • 3
  • 11
2
votes
1 answer

How to correctly send array with objects in JSON Swift iOS

Hello I am beginner and trying to understand how to send an array with objects. Does the server understand what an array is like Int, Strings or Booleans? Do you have to send the array in a string for JSON? What I do not understand? var…
handzel
  • 91
  • 1
  • 10
2
votes
1 answer

Serializing Foundation and non-foundation types to JSON in swift

I'm trying to serialize two different types of object to JSON, but I've only been able to do so using JSONEncoder for one, and JSONSerialization for the other. One is a simple Int, which I serialize as follows: let ticket: [String:Any] = ["ticket":…
kyrers
  • 489
  • 1
  • 6
  • 22
1
2 3
8 9