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
1
vote
3 answers

Json Response needs to update Javascript value and replace global initial global value - work around solution needed

I'm trying to wrap my head around the fetch api and get. From what i've been studying it looks like its suppose to be easy but my mind isn't getting it fully. In my game Javascript, it starts with a money=2000 global variable. The game plays fine,…
Ed Nolan
  • 62
  • 7
1
vote
1 answer

Swift Decodable - how to avoid generic type?

I am retrieving a complex nested object from my JSON REST API. DocumentDraft - uuid: String - schema: Schema // Very complicated object with many variations - url: String - values: [Value] - successors: [String] - predecessors:…
Mundi
  • 79,884
  • 17
  • 117
  • 140
1
vote
2 answers

Exclude CodingKeys that doesn't need to be altered?

Say I have a struct User model which has many properties in it. struct User: Codable { let firstName: String let lastName: String // many more properties... } As you can see above it conforms to Codable. Imagine if the lastName property…
Frankenstein
  • 15,732
  • 4
  • 22
  • 47
1
vote
1 answer

How does swift JSONEncoder() encode swift NSDate/ Data types?

In swift how does the JSOnEncoder() encode Data data types? Here is my struct that i am encoding struct VoiceQueryRequest: Codable { var type:String = "voiceQuery" var audioOutput:Data = Data() } Im using websockets…
Chris Maley
  • 157
  • 1
  • 8
1
vote
0 answers

JSON Dumps Better Dictionary Formatting

I would like to improve / customize json dumps to improve clarity of outputs. When I run: import json d = { 'a': 1, 'b': 2, 'c': { 'x': 9, 'y': 8, 'z': 7 }, 'd':…
perry_the_python
  • 385
  • 5
  • 14
1
vote
1 answer

typeMismatch in Decoding model again JSONEncoder and JSONDecoder swift 5

I am trying to update values in model and using JSON operation decode and encode. I have created model according to data. Everything is fine working but if I decode again my model it gives me typeMismatch error. I have tried but no success. Any one…
user12435629
1
vote
1 answer

How do you properly clear a json_encode error state?

I am using a PHP 5.6.40 development environment. Many of my classes implement JsonSerializable. The jsonSerialize method of one of these classes uses json_decode, since the value of one of its data members may or may not be encoded as JSON. The…
hutch90
  • 341
  • 3
  • 15
1
vote
0 answers

Why can't I write to the iOS App Group directory?

Beginner(?) question here. I searched StackOverflow and tried several solutions but was unable to solve this issue - I'm can't save anything to the App Group directory created after adding the App Groups capability to the target. The saving works…
flowy12
  • 11
  • 3
1
vote
1 answer

Why using wp_send_json() resolves my issue when previously using json_encode response?

I wrote a PHP class to process a CSV file and upload entries. The last round was over 15,000+ row in the file. Previously, in my wrapper I would return the json response like so: echo json_encode($jsonResponse); wp_die(); That worked fine when we…
Zach Smith
  • 5,490
  • 26
  • 84
  • 139
1
vote
2 answers

Decoding an enum containing bools

I have this JSON file. [ { "name": "January", "holidays": [ { "name": "New Year's Day", "date": "2019-01-01T00:00:00-0500", "type": { …
Isuru
  • 30,617
  • 60
  • 187
  • 303
1
vote
1 answer

Is it possible to create custom Swift KeyEncodingStrategy for enums only?

Update June 20, 2019: Thanks to @rudedog, I arrived at a working solution. I've appended implementation below my original post... Note that I am NOT looking for "use private enum CodingKeys: String, CodingKey" in your struct/enum declaration. I…
David Nedrow
  • 1,098
  • 1
  • 9
  • 26
1
vote
2 answers

JsonEncoder always return empty array

Im trying to save struct array into UserDefaults and I cant figure out why JsonEncoder return empty data. I have setup model that conforms Codable protocol struct MenuItem : Codable{ let name : String? let icon : String? init(name :…
adamvx
  • 157
  • 2
  • 10
1
vote
2 answers

Swift 4 JSONEncoder returns empty JSON array

I have multiple Codeable classes that I want to convert to JSON strings. class MyCodable: NSObject, Codable { override init() { } func encode(to encoder: Encoder) throws { } } class Category: MyCodable { required init(from…
daddygames
  • 1,880
  • 1
  • 11
  • 18
1
vote
1 answer

Crash caused by subclassing a class that inherits from Codable in Swift

My app crashes when I try to access a property of a Codable subclass instance and if one of the two conditions are met: The subclass is a multilevel child of Codable There is a function that calls JSONEncoder().encode That function doesn't have to…
rayan
  • 49
  • 4
1
vote
1 answer

Swift - JSONEncoder - encode date fields separately in a super class

I am very new to swift and I am trying to find a way to use the default encoding for a class that extends Codable and add separate data for certain fields. For example, I have a class Contact which extends Codable, with a lot of fields and I want…
Andrei F
  • 4,205
  • 9
  • 35
  • 66
1 2
3
8 9