Grabbing data from multiple form fields and creating a JSON String dynamically
By : Gray Gong
Date : March 29 2020, 07:55 AM
|
How do I use Text.JSON.Generic with optional JSON fields?
By : Rhyphte
Date : March 29 2020, 07:55 AM
it should still fix some issue This seems to be a known issue with Generics-based parsing. Aeson has had the same issue and the maintainers have decided to deprecate that functionality in favor of a Template Haskell-based strategy: https://github.com/bos/aeson/issues/118With Aeson, your code would look very similar: code :
import Data.Aeson
import Data.Aeson.TH
data Record = Record {
myMandatoryField :: Integer,
myOptionalField :: Maybe Integer
} deriving (Eq, Show)
$(deriveJSON id ''Record)
$ ghci
λ :l Main.hs
Ok, modules loaded: Main.
λ encode $ Record 5 Nothing
"{\"myOptionalField\":null,\"myMandatoryField\":5}"
λ decode it :: Maybe Record
Just (Record {myMandatoryField = 5, myOptionalField = Nothing})
instance FromJSON Record where
parseJSON = withObject "record" $ \o -> Record
<$> o .: "myMandatoryField"
<*> o .:? "myOptionalField"
instance ToJSON Record where
toJSON (Record manf optf) = object $ catMaybes
[ ("myMandatoryField" .=) <$> pure manf
, ("myOptionalField" .=) <$> optf ]
|
Avoid even Option fields. Always empty string for String and 0 for Int optional fields
By : Aleksandr
Date : March 29 2020, 07:55 AM
will help you I think you would regret avoiding Option because of the loss of type safety. If you go passing around potentially null object references, everyone who touches them has to remember to check for null because there is nothing that forces them to do so. Failure to remember is a NullPointerException waiting to happen. The use of Option forces code to deal with the possibility that there is no value to work with; forgetting to do so will cause a compilation error: code :
case class Foo(name: Option[String])
...
if (foo1.name startsWith "/") // ERROR: no startsWith on Option
trait FooJsonProtocol extends DefaultJsonProtocol with NullOptions {
// your jsonFormats
}
|
Creating a type with optional fields from variadic arguments in Go
By : user7729043
Date : March 29 2020, 07:55 AM
hop of those help? I am learning Go right now and would like to initialize a type using variadic arguments without using reflection. Is it possible? , It's possible with a little bit of code: code :
func CreateMyType(arguments ...string) *MyType {
var m MyType
switch len(arguments) {
case 3:
m.field3 = arguments[2]
fallthrough
case 2:
m.field2 = arguments[1]
fallthrough
case 1:
m.field1 = arguments[0]
}
return &m
}
|
Optional fields in creating a SP site
By : Tyrone Phelipa
Date : March 29 2020, 07:55 AM
this one helps. So you need to have the SecondaryAlies in another if/else condition like the below: code :
If($SecondaryAlias)
{
New-SPSite -Url $siteUrl -name $sitename -Language $SiteLanguage -Template $SiteTemplate -OwnerAlias $SiteOwner -SecondaryOwnerAlias $SecondaryAlias
}
else
{
New-SPSite -Url $siteUrl -name $sitename -Language $SiteLanguage -Template $SiteTemplate -OwnerAlias $SiteOwner
}
|